222. Count Complete Tree Nodes**

188 篇文章 0 订阅
26 篇文章 0 订阅

Given a complete binary tree, count the number of nodes.

Definition of a complete binary tree from Wikipedia:

In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

备注:

  • rooted binary tree has a root node and every node has at most two children.
  • full binary tree (sometimes referred to as a proper[15] or plane binary tree)[16][17] is a tree in which every node in the tree has either 0 or 2 children.
  • In the infinite complete binary tree, every node has two children (and so the set of levels is countably infinite). The set of all nodes is countably infinite, but the set of all infinite paths from the root is uncountable, having thecardinality of the continuum. These paths correspond by an order-preserving bijection to the points of the Cantor set, or (using the example of a Stern–Brocot tree) to the set of positive irrational numbers.A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depthor same level.[18] (This is ambiguously also called a complete binary tree.[19]) An example of a perfect binary tree is the ancestry chart of a person to a given depth, as each person has exactly two biological parents (one mother and one father).
  • In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes at the last level h.[19] An alternative definition is a perfect tree whose rightmost leaves (perhaps all) have been removed. Some authors use the term complete to refer instead to a perfect binary tree as defined above, in which case they call this type of tree an almost complete binary tree ornearly complete binary tree.[20][21] A complete binary tree can be efficiently represented using an array.[19]
  • balanced binary tree has the minimum possible maximum height (a.k.a. depth) for the leaf nodes, because for any given number of leaf nodes, the leaf nodes are placed at the greatest height possible.
recursive:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int countNodes(TreeNode root) {
        int h = height(root);
        return h<0?0:height(root.right)==h-1?(1<<h)+countNodes(root.right):(1<<h-1)+countNodes(root.left);

                
    }
    private int height(TreeNode root){
        return root==null?-1:height(root.left)+1;
    }
}
总结:虽然普通的遍历方法也可以计算出节点个数,但是注意这是complete tree,除叶节点外剩下的节点均有两个children,且分布于尽可能左侧。
iterative:
public class Solution {
    public int countNodes(TreeNode root) {
        int h = height(root);
        int num =0;
        while(root!=null){
            if (height(root.right)==h-1){
                num+=(1<<h);
                root=root.right;
            }
            else{
                num+=(1<<h-1);
                root=root.left;
            }
            h--;
        }
        return num;
    }
    private int height(TreeNode root){
        return root==null?-1:height(root.left)+1;
    }
}
简单粗暴的方法:
    public int countNodes(TreeNode root) {
        if(root==null) return 0;
        return 1+countNodes(root.left) +countNodes(root.right);
    }
总结:时间超时了。O(n)
改进:一遍为full tree,直接返回。
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null)
            return 0;
        TreeNode left = root, right = root;
        int height = 0;
        while (right != null) {
            left = left.left;
            right = right.right;
            height++;
        }
        if (left == null)
            return (1 << height) - 1;
        return 1 + countNodes(root.left) + countNodes(root.right);
    }
}

时间复杂度:log(n)^2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值