題目描述:輸入一棵二叉樹,求該樹的深度。從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度為樹的深度。
思路:遞歸
//遞歸 public class Solution {public int TreeDepth(TreeNode root) {if(root==null) return 0;int left=TreeDepth(root.left);int right=TreeDepth(root.right);return left>right?(left+1):(right+1);} }
?