zoukankan      html  css  js  c++  java
  • 二叉树——求一棵完全二叉树节点的个数

    已知一棵完全二叉树, 求其节点的个数
    要求: 时间复杂度低于O(N), N为这棵树的节点个数

    结论:满二叉树:高度为L,结点个数  2^L  - 1个

    先遍历左边界,求出完全二叉树的高度h

    然后遍历树的右子树的左边界,看它到没到最后一层,

      如果到了最后一层,那么证明它的左子树是满的,高度是h-1        左子树的结点数2^(h-1) - 1  + 当前节点 + 递归求解 右子树的结点的个数

      如果没到最后一层,那么证明它的右子树是满的,高度是h-2        右子树的结点数2^(h-2) - 1  + 当前节点 + 递归求解 左子树的结点的个数

    public class CompleteTreeNodeNumber {
        public static int completeTreeNodeNumber(Tree tree){
            if(tree == null) return 0;
            return comNum(tree, 1, treeHigh(tree));
        }
    
        public static int comNum(Tree tree, int level, int high){
            if(level == high) return 0;
    int l = treeHigh(tree.right); //当前结点的右孩子的最左结点到达树的最下面,则左孩子是满二叉树,高度是high-level if(l == high - level){ return comNum(tree.right, level + 1, high) + (1 << (high - level)); } else{ //当前结点的右孩子的最左结点没有到达树的最下面,则右孩子是满二叉树,高度是high-level-1 return comNum(tree.left, level + 1, high) + (1 << (high - level - 1)); } } public static int treeHigh(Tree tree){ if(tree == null) return 0; int high = 1; while(tree.left != null){ high++; tree = tree.left; } return high; } }

      

  • 相关阅读:
    CF 13B Letter A
    CF12D Ball
    题解 CF11C
    CF10E Greedy Change
    CF10D LCIS&&Acwing 272
    CF10C Digital Root
    yLOI2019 青原樱
    js有关时间日期的操作
    js 读取 cookie
    nginx有关.htaccess小结
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8947557.html
Copyright © 2011-2022 走看看