forked from Red-0111/Anything-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddonerowtotree.cpp
More file actions
28 lines (26 loc) · 789 Bytes
/
addonerowtotree.cpp
File metadata and controls
28 lines (26 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:
void helper(TreeNode* root,TreeNode* par,int val,int depth,int lr){
if(depth==0){
if(lr==1){
TreeNode* r=new TreeNode(val);
par->right=r;
r->right=root;
}
else if(lr==-1){
TreeNode* l=new TreeNode(val);
par->left=l;
l->left=root;
}
return;
}
if(!root)return;
helper(root->left,root,val,depth-1,-1);
helper(root->right,root,val,depth-1,1);
}
TreeNode* addOneRow(TreeNode* root, int val, int depth) {
if(depth==1){ TreeNode* r=new TreeNode(val); r->left=root;return r;}
helper(root,NULL,val,depth-1,0);
return root;
}
};