版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/majichen95/article/details/85370614
给定一棵完全二叉树,计算它的节点数。
样例
输入:
1
/ \
2 3
/ \ /
4 5 6
输出: 6
注意事项
在完全二叉树中,除了可能的最后一层之外,每层都被完全填充,并且最后一层中所有节点都尽可能地靠左。 最后一层h中,可能有1到2 ^ h个节点。
解题思路1:
经典的Divide Conquer。但没有用到完全二叉树的定义。
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: root of complete binary tree
* @return: the number of nodes
*/
public int countNodes(TreeNode root) {
// write your code here
if(root == null)
return 0;
int leftCount = countNodes(root.left);
int rightCount = countNodes(root.right);
return leftCount + rightCount + 1;
}
}
解题思路2:
利用树的深度信息优化。若遇到左右子树深度相同的情况,则可以直接调用公式计算结果。
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: root of complete binary tree
* @return: the number of nodes
*/
public int countNodes(TreeNode root) {
// 约定根节点的深度为1,计算左右两子树的深度
int leftDepth = countLeftDepth(root);
int rightDepth = countRightDepth(root);
if(leftDepth == rightDepth) //说明是完全二叉树
return (int)(Math.pow(2, leftDepth) - 1);
else //一般情况,则一般处理
return countNodes(root.left) + countNodes(root.right) + 1;
}
//计算以root为根最左子树的深度
private int countLeftDepth(TreeNode root){
int res = 0;
while(root != null){
res++;
root = root.left;
}
return res;
}
//计算以root为根最右子树的深度
private int countRightDepth(TreeNode root){
int res = 0;
while(root != null){
res++;
root = root.right;
}
return res;
}
}