完全二叉树的左右子树至少有一个是满二叉树
如果左子树的高度等于右子树高度+1, 那么右子树必然是一颗满二叉树
如果左子树的高度等于右子树高度,那么左子树必然是一颗满二叉树
100 28
class Solution {
public int countNodes(TreeNode root) {
if(root == null)
return 0;
int leftH = getH(root.left);
int rightH = getH(root.right);
if(leftH == rightH)
return (int)Math.pow(2,leftH) + countNodes(root.right);
else
return (int)Math.pow(2,rightH) + countNodes(root.left);
}
public int getH(TreeNode root){
int nums = 0;
while(root != null){
nums++;
root = root.left;
}
return nums;
}
}
最后
以上就是畅快枫叶最近收集整理的关于【完全二叉树节点数量】的全部内容,更多相关【完全二叉树节点数量】内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复