Loading

104. 二叉树的最大深度

104. 二叉树的最大深度

https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/

package com.test;

/**
 * @Author stono
 * @Date 2018/8/27 上午9:51
 */
public class Lesson104 {
    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2L = new TreeNode(2);
        TreeNode t2R = new TreeNode(2);
        TreeNode t3_1 = new TreeNode(3);
        TreeNode t3_2 = new TreeNode(4);
        TreeNode t3_3 = new TreeNode(4);
        TreeNode t3_4 = new TreeNode(3);
        t1.left = t2L;
        t1.right = t2R;
        t2L.left = t3_1;
        t2L.right = t3_2;
        t2R.left = t3_3;
        t2R.right = t3_4;
        int i = maxDepth(t1);
        System.out.println(i);
    }
    public static int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return 1+Math.max(maxDepth(root.left), maxDepth(root.right));
    }

}

 

posted @ 2018-08-27 10:05  stono  阅读(106)  评论(0编辑  收藏  举报