2021年9月13日17:03:42
2021年9月13日17:04:47
1.
简单方法
public class Solution { public int TreeDepth(TreeNode root) { if (root == null) return 0; int left = TreeDepth(root.left); int right = TreeDepth(root.right); return Math.max(left,right) + 1; } }
2.
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { int depth = 0; int res = 0; public int TreeDepth(TreeNode root) { depth++; if(root == null) return res; if(root.left == null && root.right == null){ int temp = depth; res = Math.max(res,temp); return res; } TreeDepth(root.left); depth--; TreeDepth(root.right); depth--; return res; } }