【一次过】Lintcode 1317. 统计完全树节点数

给定一棵完全二叉树,计算它的节点数。

样例

输入: 
    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;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值