-
Notifications
You must be signed in to change notification settings - Fork 0
/
0222.cpp
134 lines (125 loc) · 2.45 KB
/
0222.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
/*
// 层序遍历 实现
int countNodes(TreeNode* root)
{
int depth = 0;
deque<TreeNode*> que;
if (root == NULL)
return 0;
que.push_back(root);
// 最后一层结点个数
int LastNum;
while (!que.empty())
{
depth++;
int size = que.size();
LastNum = size;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop_front();
if (node->left != NULL)
que.push_back(node->left);
if (node->right != NULL)
que.push_back(node->right);
}
}
int ans = pow(2, depth - 1) - 1 + LastNum;
return ans;
}
*/
int ans;
// 先序遍历 递归 求最大深度以及相应结点数量
int countNodes(TreeNode* root)
{
// 记录每一层结点数量
vector<int> record;
ans = 0;
if (root == NULL)
return 0;
getDepth(root, 1, record);
int result = pow(2, ans - 1) - 1 + record[ans - 1];
return result;
}
// 1. 参数和返回参数
void getDepth(TreeNode* cur, int depth, vector<int>& record)
{
// 3. 单次递归处理
if (record.size() < depth)
record.push_back(int(0));
record[depth - 1]++;
// depth 是当前结点的深度
ans = ans > depth ? ans : depth; // 中
// 2. 终止条件
if (cur->left == NULL && cur->right == NULL)
return;
if (cur->left != NULL)
{
depth++;
getDepth(cur->left, depth, record); // 左
depth--;
}
if (cur->right != NULL)
{
depth++;
getDepth(cur->right, depth, record); // 右
depth--;
}
return;
}
// 利用二叉树性质
// 分别递归左孩子,和右孩子
// 递归到某一深度一定会有左孩子或者右孩子为满二叉树
// 后序遍历
int countNodes(TreeNode* root)
{
if (root == NULL)
return 0;
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftDepth = 0;
int rightDepth = 0;
while (left)
{
left = left->left;
leftDepth++;
}
while (right)
{
right = right->right;
rightDepth++;
}
if (leftDepth == rightDepth)
{
return pow(2, leftDepth + 1) - 1;
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
};
int main()
{
TreeNode* root = new TreeNode(1);
// TreeNode* node = root;
// node->left = new TreeNode(2);
// node->right = new TreeNode(3);
// node->left->left = new TreeNode(4);
// node->left->right = new TreeNode(5);
// node->right->right = new TreeNode(7);
Solution S;
S.countNodes(root);
return 0;
}