import java.util.*; public class Solution { public int TreeDepth(TreeNode root) { if(root == null) return 0; int l = TreeDepth(root.left); int r = TreeDepth(root.right); return Math.max(l, r) + 1; } }
简写
import java.util.*; public class Solution { public int maxDepth (TreeNode root) { //空节点没有深度 if(root == null) return 0; //返回子树深度+1 return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; } }