Count Complete Tree Nodes

/**
* 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:
void traverseLeft(TreeNode* root, int& h)
{
if(root == NULL)
{
return;
}
++ h;
traverseLeft(root -> left, h);

}
void traverseRight(TreeNode* root, int& h)
{
if(root == NULL)
{
return;
}
++ h;
traverseRight(root -> right, h);
}

int countNodes(TreeNode* root)
{
if(root == NULL)
{
return 0;
}
int h1 = 1;
int h2 = 1;
traverseLeft(root -> left, h1);
traverseRight(root -> right, h2);
if(h1 == h2)
{
return pow(2, h1) - 1;
}
//int h = max(h1, h2);

return countNodes(root -> left) + countNodes(root -> right) + 1;

}
};

leetcode 222: Count Complete Tree Nodes

2015-06-06 05:14:48

Leetcode[222]-Count Complete Tree Nodes

2015-06-11 13:24:18

[leetcode] 222. Count Complete Tree Nodes 解题报告

2016-01-12 06:57:17

[LeetCode] Count Complete Tree Nodes

2015-06-06 13:45:35

LeetCode222:Count Complete Tree Nodes

2015-07-29 17:17:00

LeetCode 222. Count Complete Tree Nodes 题解——Java

2017-03-15 09:14:24

LeetCode222 Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解

2015-07-09 20:51:26

leetcode 222:Count Complete Tree Nodes

2015-09-26 23:12:55

Count Complete Tree Nodes(****)

2015-08-12 21:58:30

Paths in a Complete Binary Tree codeforces

2017-04-04 23:19:02

不良信息举报

Count Complete Tree Nodes