最大二叉树的深度

最大二叉树的深度

给出一个二叉树,找出其最大的深度,深度是指从二叉树的根节点到叶子节点的节点数


Given binary tree [3,9,20,null,null,15,7],

return its depth = 3.


思路:最简单的方式就是递归,递归求得根节点左子树的深度和右子树的深度,再进行比较得到最大的深度。

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

猜你喜欢

转载自blog.csdn.net/w1375834506/article/details/88830089