Skip to content

Commit

Permalink
Merge pull request #627 from vishwajeet-hogale/main
Browse files Browse the repository at this point in the history
Oct 5th leetcode problem : Add one row to tree
  • Loading branch information
gantavyamalviya authored Oct 5, 2022
2 parents f256b14 + e7548cd commit 9210e8c
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion Trees/addOneRowToTree.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,83 @@

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct TreeNode{
int val;
TreeNode *left,*right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

/*
Intuition
Use queues to peform bfs and traverse the tree. When you reach depth - 1 ,
perform the operations mentioned in the question
Approach
There are two cases when you reach depth - 1 using bfs.
Assume curr is the current node at depth - 1
* curr->left != NULL
* curr->left == NULL
The code handles the above two cases.
There is also one edge case that the code can't handle.
If the depth is 1 which means we need to add the new_node as the root node.
So we have to write a seperate code to handle depth = 1 case.
*/
TreeNode* addOneRow(TreeNode* root, int val, int depth) {

int d = 0;
if(d == (depth - 1)){
TreeNode *new_node = new TreeNode(val);
new_node->left = root;
root = new_node;
return root;
}
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
d++;
for(int i=0;i<size;i++){
TreeNode *curr = q.front();
q.pop();
if(!curr)
continue;
if(d == (depth - 1)){
TreeNode *new_node = new TreeNode(val);
TreeNode *new_node1 = new TreeNode(val);
if(curr->left){
TreeNode *temp = curr->left;
new_node->left = temp;
curr->left = new_node;
}
else{
curr->left = new_node;
}
if(curr->right){
TreeNode *temp = curr->right;
new_node1->right = temp;
curr->right = new_node1;
}
else{
curr->right = new_node1;
}

}
if(curr->left)
q.push(curr->left);
if(curr->right)
q.push(curr->right);

}
if(d == (depth-1))
break;
}
return root;
}
=======
/*
623. Add One Row to Tree
Expand Down Expand Up @@ -59,4 +139,5 @@ class Solution {
dfs(root,val,depth,1);
return root;
}
};
};

0 comments on commit 9210e8c

Please sign in to comment.