面试题39:二叉树的深度
2019-03-25 本文已影响0人
liuqinh2s
/**
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) {
return recurse(root, 0);
}
private int recurse(TreeNode node, int depth){
if(node==null){
return depth;
}
int leftDepth = recurse(node.left, depth+1);
int rightDepth = recurse(node.right, depth+1);
return leftDepth>rightDepth?leftDepth:rightDepth;
}
}