您的当前位置:首页正文

如何求取二叉树最长路径的长度

2020-11-27 来源:步旅网

解题思路:递归算法

/**
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;
 }
}
显示全文