LeetCode-222.完全二叉树的节点个数

. - 力扣(LeetCode)

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

思路1:二叉树性质求解,遍历每一个节点。

        递归

                1、确定递归函数的参数和返回值:参数:根节点,返回值:节点数interesting

int CountNodes(TreeNode* root){}

                2、确定终止条件:如果为空节点的话,就返回0,表示节点数为0。

if (root == NULL) return 0;

                3、确定单层递归逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。

int leftNum = CountNodes(root->left);      // 左
int rightNum = CountNodes(root->right);    // 右
int treeNum = leftNum + rightNum + 1;      // 中
return treeNum;
int CountNodes(TreeNode* root){
    if (root == NULL) return 0;

    int leftNum = CountNodes(root->left);      // 左
    int rightNum = CountNodes(root->right);    // 右
    int treeNum = leftNum + rightNum + 1;      // 中
    
    return treeNum;
}
// 简化版本
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == NULL) return 0;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};

迭代:二叉树层序遍历(队列)

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if(root != nullptr)
            que.push(root);
        int res = 0;
        while(!que.empty()){
            int size = que.size();
            for(int i = 0; i < size; i++){
                TreeNode* node = que.front();
                que.pop();
                res++;
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
            }
        }
        return res;
    }
};

思路2:满二叉树性质。左0右1,二进制排列,最后一个节点的二进制数就是总节点数。

已知满二叉树,则书高度由左子树深度h决定,所以深度为h的满二叉树的节点总数= 2^h-1,(等比数列求和 (1-2^h)/(1-2) = 2^h-1)。若最底层为第 h 层,则该层节点二进制序号(2^h ~ 2^(h+1))。

求解:总节点数 == h层最后一个节点

      1)遍历左子树,得到树高h,

      2)二分法确定叶子节点的个数(2^h ~ 2^(h+1))(根节点在第0层)

      3)位运算判断叶子节点是否存在。按照二进制编码,左孩子0,右孩子1(第12个节点,二进制表示1100),则2^h每次左移一位位与第k节点,该位为1则右子树,为0则左子树。若节点为空或者2^h左移为0结束循环,返回节点是否为空

class Solution {
public:
    bool exits(TreeNode* root, int h, int k ){
        int bits = 1 << (h-1);   // 1<<(h-1) == 2^h
        TreeNode* node = root;
        while(node != nullptr && bits > 0){
            // 该数二进制位,左子树为0,右子树为1
            // 按位相与,该位同1为1,2&3 == 010 & 011 == 010 == 2
            if( !(bits & k))  // 检查当前位是否为1
                node = node->left; //bits & k == 0,即该位为0
            else
                node = node->right;

            bits >>= 1; // 判断下一位
        }
        return node != nullptr;

    }
    int countNodes(TreeNode* root) {
        if(root == nullptr) return 0;
        
        int h = 0; // 默认根节点为第0层
        TreeNode* node = root;
        while(node->left != nullptr){
            h++;
            node = node->left;
        }
        
        int l = 1 << h, r = (1 << (h+1)) - 1;
        while(l < r){
            int mid = (r - l + 1) / 2 + l; // 求中间数,(r+l)/2会有溢位风险
            if(exits(root, h, mid)){
                l = mid;
            }else{
                r = mid - 1; // mid位置没有节点,所以-1
            }
        }
        return l;

    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值