输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
解题思路:递归算法
/**public class treenode { int val = 0; treenode left = null; treenode right = null; public treenode(int val) { this.val = val; }}*/import java.lang.math;public class solution { public int treedepth(treenode proot) { if(proot == null){ return 0; } int left = treedepth(proot.left); int right = treedepth(proot.right); return math.max(left, right) + 1; }}
以上就是如何求取二叉树最长路径的长度的详细内容。
