222. Count Complete Tree Nodes**

来源:互联网 发布:知行结合人物论据 编辑:程序博客网 时间:2024/05/16 12:34

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

0 0
原创粉丝点击