-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101. Symmetric Tree.cpp
115 lines (85 loc) · 3.16 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
/*
Link: https://leetcode.com/problems/symmetric-tree/
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(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// Recursive Approach
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) // if root is null then tree is symmetric
return true;
return check(root -> right, root -> left);
}
bool check(TreeNode* root1, TreeNode* root2) { // given the left and right node we check
if(!root1 && !root2) { // if both the nodes are null
return true;
}
if(!root1 || !root2) // either any of the node is not null and other one is.
return false;
return root1 -> val == root2 -> val // checking for equality of the value of the nodes
and check(root1 -> right, root2 -> left) // call checking for left and right of the subtree
and check(root1 -> left, root2 -> right);
}
};
/*
TC = O(n) where n is the number of nodes
SC = O(n)
*/
// Iterative Approach
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root)
return true;
// Here we use queue to store the left and right of the node and then check their value
queue<TreeNode *> q;
q.push(root); // inserting root into the queue
q.push(root); // we can insert the left and right node the root here as well
while (!q.empty()) {
TreeNode *a = q.front(); // accessing the front node of the queue
q.pop(); // poping out the first element
TreeNode *b = q.front(); // same as above
q.pop();
if(a && b) { // if both the node exists
if (a -> val != b -> val) { // if the value of the nodes aren't equal
return false;
}
q.push(a -> right); // first pushing right of a then left of b, and then vice-versa
q.push(b -> left);
q.push(a -> left);
q.push(b -> right);
} else if (!a && !b) // if both the node null then we must continue untill the queue is empty
continue;
else {
return false; // if any of the node is not null and other is null
}
}
return true;
}
};
/*
TC = O(n) where n is the number of nodes
SC = O(n)
*/