104. 二叉树的最大深度

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


求深度的题,最好是从叶子向上回溯。

    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left,right) +1;

    }
posted @ 2022-02-23 14:06  一颗青菜  阅读(3)  评论(0)    收藏  举报