`

Leetcode - Count Complete Tree 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.

[分析]
[思路1]直接递归时间复杂度是2^h 级别,超时,需要些剪枝技巧:如果从当前节点一直往左的高度 == 一直往右的高度,则以当前节点为根的树是一颗perfect tree,包含的节点数 = 2^h - 1, 其中h 为根节点高度记为1时的树高;否则递归调用count(root.left) + count(root.right) + 1 来计算。剪枝后算法复杂度为h^2,h 为数高,可以这样理解:第一次检查左右子树高度时,高度为h,计算量时O(h),若有第二次,待检查的树高减一,计算量为O(h - 1)……如此递推,因此平均时间复杂度是O(h^2)。
[思路2] 二分枚举。高度为 h 的完全二叉树,其节点数=高度为 h - 1的满二叉树 + 最后一层节点数。如何获取最后一层节点数呢?h 从0开始计,则最后一层满时节点数=2^h。给最后一层几点编号,若记往左走为0,往右走为1,则最后一层节点编号二进制形式正好对应从根节点开始的路径。二分枚举节点并判断其是否存在,直到找到最后一层的最右边节点。
搬一个参考博客上的图:
Lv0        1
         /    \
Lv1     2      3
       /  \   /  \
Lv2   4   5  6   -

No.   00  01 10


[ref]
http://bookshadow.com/weblog/2015/06/06/leetcode-count-complete-tree-nodes/

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    // Method 1: O(h^2)
    public int countNodes1(TreeNode root) {
        if (root == null)
            return 0;
        if (root.left == null && root.right == null)
            return 1;
        TreeNode curr = root;
        int lHeight = 0;
        while (curr.left != null) {
            lHeight++;
            curr = curr.left;
        }
        curr = root;
        int rHeight = 0;
        while (curr.right != null) {
            rHeight++;
            curr = curr.right;
        }
        if (lHeight == rHeight) {
            return (1 << (lHeight + 1)) - 1;
        } else {
            return countNodes(root.left) + countNodes(root.right) + 1; 
        }
    }
    // Method 2: Binary Search
    public int countNodes(TreeNode root) {
        if (root == null)
            return 0;
        int height = 0;
        TreeNode curr = root;
        while (curr.left != null) {
            height++;
            curr = curr.left;
        }
        int left = 0, right = (1 << height) - 1;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if (existsNode(root, mid, height - 1)) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return (1 << height) + right;
    }
    public boolean existsNode(TreeNode root, int path, int height) {
        while (height >= 0 && root != null) {
            if (((path >> height) & 1) == 1) {
                root = root.right;
            } else {
                root = root.left;
            }
            height--;
        }
        return root != null;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics