思路:递归(代码简单但是真的难懂)
代码:
/** 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) { if(root==null) return 0; int nleft = TreeDepth(root.left); int nright = TreeDepth(root.right); return Math.max(nleft,nright)+1; } }
原文:https://www.cnblogs.com/loyolh/p/12890237.html