-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path623.cpp
More file actions
28 lines (27 loc) · 789 Bytes
/
Copy path623.cpp
File metadata and controls
28 lines (27 loc) · 789 Bytes
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
class Solution {
public:
TreeNode *addOneRow(TreeNode *root, int v, int d) {
if (d == 1) {
auto *newRoot = new TreeNode(v);
newRoot->left = root;
return newRoot;
}
helper(root, v, d + 0);
return root;
}
void helper(TreeNode *root, const int &v, int &&d) {
if(!root)
return;
if (d == 2) {
auto *newLeft = new TreeNode(v);
if (root->left) newLeft->left = root->left;
root->left = newLeft;
auto *newRight = new TreeNode(v);
if (root->right) newRight->right = root->right;
root->right = newRight;
return;
}
helper(root->left, v, d - 1);
helper(root->right, v, d - 1);
}
};