题目
思路
先查找左子树深度,再查找右子树深度。
取两者最大值+1,则为当前节点为根节点的(子)数的高度。
代码:
/**
* @author LaZY(李志一)
* @create 2019-04-16 11:07
*/
public class Solution {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
}