LeetCode104 Maximum Depth of Binary Tree

idea: the maxDepth of current Node is the maxDepth between its left subtree and right subtree plus 1, so it’s max(l, r) + 1.

class Solution {
    public int maxDepth(TreeNode root) { //the maxDepth under current node as rude, think this straight and it's very easy
        if(root == null) return 0;
        int l = maxDepth(root.left);
        int r = maxDepth(root.right);
        return Math.max(l, r) + 1;
    }
    
}

and of course we can use BFS to get the max depth.

posted @ 2020-11-04 11:41  EvanMeetTheWorld  阅读(16)  评论(0)    收藏  举报