-
Notifications
You must be signed in to change notification settings - Fork 0
/
addonerow_tree.cpp
49 lines (49 loc) · 1.34 KB
/
addonerow_tree.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
/**
* 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:
TreeNode* addOneRow(TreeNode* root, int v, int d) {
int level = 1;
if (d == 1) {
TreeNode *neo = new TreeNode(v);
neo->left = root;
root = neo;
}
else {
queue<TreeNode *> Q;
Q.push(root);
TreeNode *curr;
int i,s;
while (level < d-1) {
s = Q.size();
for (i = 0; i < s; i++) {
curr = Q.front();
Q.pop();
if (curr->left != NULL)
Q.push(curr->left);
if (curr->right != NULL)
Q.push(curr->right);
}
level++;
}
while (!Q.empty()) {
TreeNode *neo_l = new TreeNode(v);
TreeNode *neo_r = new TreeNode(v);
curr = Q.front();
Q.pop();
neo_l->left = curr->left;
neo_r->right = curr->right;
curr->left = neo_l;
curr->right = neo_r;
}
}
return root;
}
};