二叉树深度
二叉树的深度
http://www.nowcoder.com/questionTerminal/435fb86331474282a3499955f0a41e8b
思路分析:
递归结束条件:节点为空,则其深度为0
否则以root为根节点的子树的深度等于1加上左右子树的深度大的那一个
public class Solution {
public int TreeDepth(TreeNode root) {
if(root == null){
return 0;
}
int leftLength = TreeDepth(root.left);
int rightLength = TreeDepth(root.right);
if(leftLength > rightLength){
return 1 + leftLength;
}
return 1 + rightLength;
}
} 