关于leetcode个人解题总结:刷题18二叉树的最大深度

7次阅读

共计 244 个字符,预计需要花费 1 分钟才能阅读完成。

Leetcode:104. 二叉树的最大深度

要点:利用递归,只看一个头节点须要做哪些事。本题中,头节点只需看它的左子树和右子树哪个的深度最大,取其中最大的深度,再在最大深度根底上 + 1 就是本节点的最大深度。

class Solution {public int maxDepth(TreeNode root) {if(root == null) return 0;
        int l = maxDepth(root.left);
        int r = maxDepth(root.right);
        return Math.max(l,r) + 1;
    }
}
正文完
 0