-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path222. Count Complete Tree Nodes.c
58 lines (44 loc) · 1.21 KB
/
222. Count Complete Tree Nodes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
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 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;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void dfs(struct TreeNode *node, int i, int n, int *x) {
if (i == n) {
if (node->left) (*x) ++;
if (node->right) (*x) ++;
return;
}
if ((*x) % 2) return;
dfs(node->left, i + 1, n, x);
if ((*x) % 2) return;
dfs(node->right, i + 1, n, x);
}
int countNodes(struct TreeNode* root) {
struct TreeNode *node = root;
int i = 0;
int x = 0;
if (!node) return 0;
while (node) {
i ++;
node = node->right;
}
dfs(root, 1, i, &x);
return (1 << i) - 1 + x;
}
/*
Difficulty:Medium
Total Accepted:64.8K
Total Submissions:235.8K
Related Topics Tree Binary Search
Similar Questions Closest Binary Search Tree Value
*/