-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckIfBinaryTreeIsTheMirrorOfItselfOrNot.cpp
55 lines (51 loc) · 1.54 KB
/
CheckIfBinaryTreeIsTheMirrorOfItselfOrNot.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
50
51
52
53
54
55
// Q129
//Covert the tree into its mirror
// https://practice.geeksforgeeks.org/problems/mirror-tree/1
class Solution {
public:
// Function to convert a binary tree into its mirror tree.
void mirror(Node* node) {
if(node==NULL) return;
mirror(node->left);
mirror(node->right);
swap(node->left,node->right);
}
};
// Invert the binary tree
// https://www.codingninjas.com/codestudio/problems/invert-a-binary-tree_1281382?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website
#include<bits/stdc++.h>
bool helper(TreeNode<int> *root,TreeNode<int> *leaf,stack<TreeNode<int> *> &st){
st.push(root);
if(root->left==NULL&&root->right==NULL){ //root is leaf node
if(root->data==leaf->data) return true; //root is same value as leaf
else {st.pop(); return false;}
}
if(root->left && helper(root->left,leaf,st)) return true;
if(root->right && helper(root->right,leaf,st)) return true;
st.pop();
return false;
}
TreeNode<int> * invertBinaryTree(TreeNode<int> *root, TreeNode<int> *leaf)
{
if(!root) return NULL;
stack<TreeNode<int> *> st;
helper(root,leaf,st);
TreeNode<int> *newr=st.top();
st.pop();
TreeNode<int> *par=newr; //parent
while(!st.empty()){
auto cur=st.top();
st.pop();
if(cur->left==par){
cur->left=NULL;
par->left=cur;
}
else{
cur->right=cur->left;
cur->left=NULL;
par->left=cur;
}
par=cur;
}
return newr;
}