From de339fb768c4350f34bf170f4bafe81bb14a7796 Mon Sep 17 00:00:00 2001 From: 3NCRY9T3D <1905294@kiit.ac.in> Date: Wed, 5 Oct 2022 10:08:34 +0530 Subject: [PATCH] Add One Row to Tree --- Add One Row to Tree.cpp | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Add One Row to Tree.cpp diff --git a/Add One Row to Tree.cpp b/Add One Row to Tree.cpp new file mode 100644 index 0000000..e97c658 --- /dev/null +++ b/Add One Row to Tree.cpp @@ -0,0 +1,76 @@ +#include +#include + + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + }; + +class Solution { +public: + void solve(TreeNode* root,int val,int depth) + { + int l=2; + queue q; + q.push(root); + while(!q.empty()) + { + int n=q.size(); + for(int i=0;ileft) + { + TreeNode* temp=curr->left; + curr->left=n1; + n1->left=temp; + } + else + curr->left=n1; + if(curr->right) + { + TreeNode* temp=curr->right; + curr->right=n2; + n2->right=temp; + } + else + curr->right=n2; + } + else + { + if(curr->left) + q.push(curr->left); + if(curr->right) + q.push(curr->right); + } + } + l++; + if(l>depth) + return; + } + } + TreeNode* addOneRow(TreeNode* root, int val, int depth) { + if(depth==1) + { + TreeNode* newroot=new TreeNode(val); + newroot->left=root; + return newroot; + } + solve(root,val,depth); + return root; + } +}; + +int main(){ + + return 0; +} \ No newline at end of file