思路:使用递归分治的思想,首先我们可以递归的求出左子树的深度,其次我们可以求出右子树的深度,然后取最大值即可。
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
public int TreeDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1;
}
} 
京公网安备 11010502036488号