一、题目
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度
二、解法
1 /** 2 public class TreeNode { 3 int val = 0; 4 TreeNode left = null; 5 TreeNode right = null; 6 7 public TreeNode(int val) { 8 this.val = val; 9 10 } 11 12 } 13 */ 14 public class Solution { 15 public int TreeDepth(TreeNode root) { 16 if(root == null) 17 return 0; 18 int left = TreeDepth(root.left); 19 int right = TreeDepth(root.right); 20 return (left > right)?(left+1):(right+1); 21 } 22 }