zoukankan      html  css  js  c++  java
  • LeetCode 222.完全二叉树的节点个数(C++)

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

    说明:

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

    示例:

    输入: 
        1
       / 
      2   3
     /   /
    4  5 6
    
    输出: 6

    转别人解法:
    class Solution {
        public int countNodes(TreeNode root) {
            /**
            完全二叉树的高度可以直接通过不断地访问左子树就可以获取
            判断左右子树的高度: 
            如果相等说明左子树是满二叉树, 然后进一步判断右子树的节点数(最后一层最后出现的节点必然在右子树中)
            如果不等说明右子树是深度小于左子树的满二叉树, 然后进一步判断左子树的节点数(最后一层最后出现的节点必然在左子树中)
            **/
            if (root==null) return 0;
            int ld = getDepth(root.left);
            int rd = getDepth(root.right);
            if(ld == rd) return (1 << ld) + countNodes(root.right); // 1(根节点) + (1 << ld)-1(左完全左子树节点数) + 右子树节点数量
            else return (1 << rd) + countNodes(root.left);  // 1(根节点) + (1 << rd)-1(右完全右子树节点数) + 左子树节点数量
            
        }
    
        private int getDepth(TreeNode r) {
            int depth = 0;
            while(r != null) {
                depth++;
                r = r.left;
            }
            return depth;
        }
    }

    我用遍历解的:

    先序/中序/后序

    class Solution {
    public:
        int countNodes(TreeNode* root) {
            if (root) {
                    ++n;
            countNodes(root->left);
                    //++n;
            countNodes(root->right);
                    //++n;
            }
            return n;
        }
    private:
        int n = 0;
    };

    层次遍历:

    class Solution {
    public:
        int countNodes(TreeNode* root) {
        queue<TreeNode*> Q;
        TreeNode* temp = root;
        if (temp != NULL)
            Q.push(temp);
        while (!Q.empty()) {
            temp = Q.front();
            ++n;
            Q.pop();
            if (temp->left)
                Q.push(temp->left);
            if (temp->right)
                Q.push(temp->right);
            }
        return n;
        }
    private:
        int n = 0;
    };
     
  • 相关阅读:
    BZOJ 3251 树上三角形:LCA【构成三角形的结论】
    BZOJ 2442 [Usaco2011 Open]修剪草坪:单调队列优化dp
    2018湖南省赛选拔
    扩展BSGS-传送门
    倒数第N个字符串
    HDU-6070 Dirt Ratio(二分+线段树+分数规划)
    第一场多校
    HDU5923-Prediction-有继承味道的并查集
    POJ2516费用流
    POJ3436:ACM Computer Factory-最大流
  • 原文地址:https://www.cnblogs.com/Mayfly-nymph/p/10548880.html
Copyright © 2011-2022 走看看