public class Solution {
public int run(TreeNode root) {
if(root == null) return 0;
if(root.left == null && root.right != null){
return run(root.right) + 1;
}
if(root.left != null && root.right == null){
return run(root.left) + 1;
}
return Math.min(run(root.left),run(root.right))+1;
}
}
太简洁了,太nb了
本文介绍了一种简洁高效的计算二叉树深度的方法。通过递归遍历左右子树并取最小值加一的方式,实现了对二叉树高度的有效测量。此算法特别适用于不平衡二叉树,能够快速找到最短路径的长度。

被折叠的 条评论
为什么被折叠?



