二叉树的深度

2020-07-28  本文已影响0人  Crazy_Bear
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public int TreeDepth(TreeNode root) {
        if(root== null) return 0;
        if(root.left == null && root.right ==null) return 1;
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        return 1+Math.max(left, right);
    }
}
上一篇下一篇

猜你喜欢

热点阅读