【Count Complete Tree Nodes】cpp

题目:

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.

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int countNodes(TreeNode* root) {
            if ( !root ) return 0;
            int hl = 1;
            TreeNode* l = root->left;
            int hr = 1;
            TreeNode* r = root->right;
            while ( l ){
                ++hl;
                l = l->left;
            }
            while ( r ){
                ++hr;
                r = r->right;
            }
            if ( hr==hl ) return pow(2, hr)-1;
            return Solution::countNodes(root->left) + Solution::countNodes(root->right) + 1;
    }
};

tips:

学习大神的递归代码:http://bookshadow.com/weblog/2015/06/06/leetcode-count-complete-tree-nodes/

(1)如果left height=right height则是full tree可以直接返回节点数

(2)如果left height!=right height则不是full tree,分别递归求解left和right的节点数

时间: 2024-07-30 14:07:44

【Count Complete Tree Nodes】cpp的相关文章

【LeetCode】222. Count Complete Tree Nodes

Count Complete Tree Nodes 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 le

8.8 LeetCode 222 Count Complete Tree Nodes

Question: Count Complete Tree Nodes Total Accepted: 11040 Total Submissions: 53992My Submissions Question Solution Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree

[LeetCode][JavaScript]Count Complete Tree Nodes

Count Complete Tree Nodes 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 le

【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes

解法一:递归 int countNodes(TreeNode* root) { if (root == NULL) return 0; TreeNode *pLeft = root->left; TreeNode *pRight = root->right; int ldepth = 0, rdepth = 0; while (pLeft) { ldepth++; pLeft = pLeft->left; } while (pRight) { rdepth++; pRight = pRi

leetCode(19):Count Complete Tree Nodes

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 po

[LeetCode] 222. Count Complete Tree Nodes Java

题目: 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

Count Complete Tree Nodes

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 pos

[leedcode 222] Count Complete Tree Nodes

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 pos

LeetCode——Count Complete Tree Nodes

Description: Given a complete binary tree, count the number of nodes. 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 2hnode