forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101. Symmetric Tree.cpp
140 lines (104 loc) · 3.65 KB
/
101. Symmetric Tree.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
Follow up: Solve it both recursively and iteratively.
/**
* Definition for a binary tree node.
* 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:
bool isSymmetricUtil(TreeNode *root1, TreeNode *root2){
if(root1==NULL && root2==NULL) return true;
if(root1==NULL || root2==NULL) return false;
return (root1->val==root2->val && isSymmetricUtil(root1->left, root2->right) && isSymmetricUtil(root1->right, root2->left));
}
bool isSymmetric(TreeNode* root) {
TreeNode *root1=root, *root2=root;
return isSymmetricUtil(root1, root2);
}
};
/*
For two trees to be mirror images, the following three conditions must be true
1 - Their root node's key must be same
2 - left subtree of left tree and right subtree of right tree have to be mirror images
3 - right subtree of left tree and left subtree of right tree have to be mirror images
*/
// Iterative
/**
* Definition for a binary tree node.
* 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:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
if(!root->left && !root->right) return true;
queue<TreeNode*> q;
// Add root to queue two times so that
// it can be checked if either one child
// alone is NULL or not.
q.push(root);
q.push(root);
while(!q.empty()){
TreeNode *leftNode=q.front();
q.pop();
TreeNode *rightNode=q.front();
q.pop();
if(leftNode->val!=rightNode->val) return false;
// Push left child of left subtree node
// and right child of right subtree node in queue.
if(leftNode->left && rightNode->right){
q.push(leftNode->left);
q.push(rightNode->right);
}
// If only one child is present alone
// and other is NULL, then tree is not symmetric.
else if(leftNode->left || rightNode->right){
return false;
}
if(leftNode->right && rightNode->left){
q.push(leftNode->right);
q.push(rightNode->left);
}
else if(leftNode->right || rightNode->left){
return false;
}
}
return true;
}
};
/*
Note that for a symmetric, the elements at every level are palindromic.
In other words,
1. The left child of left subtree = right child of right subtree.
2. The right child of left subtree = left child of right subtree.
If we insert the left child of left subtree first followed by right child of the right subtree in the queue,
we only need to ensure that these are equal.
Similarly, If we insert the right child of left subtree followed by left child of the right subtree in the queue,
we again need to ensure that these are equal.
*/