Skip to content

Commit 8be001c

Browse files
committed
🚀 26-Aug-2020
1 parent f964911 commit 8be001c

13 files changed

+1194
-0
lines changed

competitive programming/leetcode/100. Same Tree.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,32 @@ class Solution {
5959
else return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
6060
}
6161
};
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
/**
72+
* Definition for a binary tree node.
73+
* struct TreeNode {
74+
* int val;
75+
* TreeNode *left;
76+
* TreeNode *right;
77+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
78+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
79+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
80+
* };
81+
*/
82+
class Solution {
83+
public:
84+
bool isSameTree(TreeNode* p, TreeNode* q) {
85+
if(p==NULL && q==NULL) return true;
86+
if(p==NULL || q==NULL) return false;
87+
88+
return (p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
89+
}
90+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
2+
3+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
4+
5+
1
6+
/ \
7+
2 2
8+
/ \ / \
9+
3 4 4 3
10+
11+
12+
But the following [1,2,2,null,3,null,3] is not:
13+
14+
1
15+
/ \
16+
2 2
17+
\ \
18+
3 3
19+
20+
21+
Follow up: Solve it both recursively and iteratively.
22+
23+
24+
25+
26+
27+
28+
29+
30+
/**
31+
* Definition for a binary tree node.
32+
* struct TreeNode {
33+
* int val;
34+
* TreeNode *left;
35+
* TreeNode *right;
36+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
37+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
38+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
39+
* };
40+
*/
41+
class Solution {
42+
public:
43+
44+
bool isSymmetricUtil(TreeNode *root1, TreeNode *root2){
45+
if(root1==NULL && root2==NULL) return true;
46+
if(root1==NULL || root2==NULL) return false;
47+
48+
return (root1->val==root2->val && isSymmetricUtil(root1->left, root2->right) && isSymmetricUtil(root1->right, root2->left));
49+
}
50+
51+
bool isSymmetric(TreeNode* root) {
52+
TreeNode *root1=root, *root2=root;
53+
return isSymmetricUtil(root1, root2);
54+
}
55+
};
56+
57+
58+
59+
60+
/*
61+
For two trees to be mirror images, the following three conditions must be true
62+
1 - Their root node's key must be same
63+
2 - left subtree of left tree and right subtree of right tree have to be mirror images
64+
3 - right subtree of left tree and left subtree of right tree have to be mirror images
65+
*/
66+
67+
68+
69+
70+
71+
// Iterative
72+
73+
74+
/**
75+
* Definition for a binary tree node.
76+
* struct TreeNode {
77+
* int val;
78+
* TreeNode *left;
79+
* TreeNode *right;
80+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
81+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
82+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
83+
* };
84+
*/
85+
class Solution {
86+
public:
87+
bool isSymmetric(TreeNode* root) {
88+
if(!root) return true;
89+
if(!root->left && !root->right) return true;
90+
queue<TreeNode*> q;
91+
92+
// Add root to queue two times so that
93+
// it can be checked if either one child
94+
// alone is NULL or not.
95+
q.push(root);
96+
q.push(root);
97+
98+
while(!q.empty()){
99+
TreeNode *leftNode=q.front();
100+
q.pop();
101+
TreeNode *rightNode=q.front();
102+
q.pop();
103+
104+
if(leftNode->val!=rightNode->val) return false;
105+
106+
// Push left child of left subtree node
107+
// and right child of right subtree node in queue.
108+
if(leftNode->left && rightNode->right){
109+
q.push(leftNode->left);
110+
q.push(rightNode->right);
111+
}
112+
// If only one child is present alone
113+
// and other is NULL, then tree is not symmetric.
114+
else if(leftNode->left || rightNode->right){
115+
return false;
116+
}
117+
118+
if(leftNode->right && rightNode->left){
119+
q.push(leftNode->right);
120+
q.push(rightNode->left);
121+
}
122+
else if(leftNode->right || rightNode->left){
123+
return false;
124+
}
125+
}
126+
return true;
127+
}
128+
};
129+
130+
131+
/*
132+
Note that for a symmetric, the elements at every level are palindromic.
133+
In other words,
134+
1. The left child of left subtree = right child of right subtree.
135+
2. The right child of left subtree = left child of right subtree.
136+
If we insert the left child of left subtree first followed by right child of the right subtree in the queue,
137+
we only need to ensure that these are equal.
138+
Similarly, If we insert the right child of left subtree followed by left child of the right subtree in the queue,
139+
we again need to ensure that these are equal.
140+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* struct TreeNode {
17+
* int val;
18+
* TreeNode *left;
19+
* TreeNode *right;
20+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
21+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
22+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
23+
* };
24+
*/
25+
26+
27+
// Recursive Approach might give Heap Buffer Overflow Error
28+
29+
class Solution {
30+
public:
31+
32+
unordered_map<int, int> mp;
33+
34+
TreeNode* constructTree(vector<int> &preorder, vector<int> &inorder, int start, int end){
35+
static int pIndex=0;
36+
37+
if(start>end) return NULL;
38+
39+
TreeNode* tNode= new TreeNode(preorder[pIndex++]);
40+
41+
if(start==end) return tNode;
42+
43+
int index=mp[tNode->val];
44+
45+
tNode->left=constructTree(preorder, inorder, start, index-1);
46+
tNode->right=constructTree(preorder, inorder, index+1, end);
47+
48+
return tNode;
49+
50+
}
51+
52+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
53+
54+
int n=inorder.size();
55+
56+
// Storing data in map for O(1) searching
57+
for(int i=0;i<n;i++)
58+
mp[inorder[i]]=i;
59+
60+
return constructTree(preorder, inorder, 0, n-1); // <preorder, inorder, start, end>
61+
}
62+
};
63+
64+
65+
66+
67+
// Follow Up-----> iterative Approach
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Given inorder and postorder traversal of a tree, construct the binary tree.
2+
3+
Note:
4+
You may assume that duplicates do not exist in the tree.
5+
6+
For example, given
7+
8+
inorder = [9,3,15,20,7]
9+
postorder = [9,15,7,20,3]
10+
Return the following binary tree:
11+
12+
3
13+
/ \
14+
9 20
15+
/ \
16+
15 7
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
/**
27+
* Definition for a binary tree node.
28+
* struct TreeNode {
29+
* int val;
30+
* TreeNode *left;
31+
* TreeNode *right;
32+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
33+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
34+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
35+
* };
36+
*/
37+
class Solution {
38+
public:
39+
40+
unordered_map<int, int> mp;
41+
42+
TreeNode* constructTree(vector<int> &postorder, vector<int> &inorder, int start, int end){
43+
44+
static int pIndex=end;
45+
if(start>end) return NULL;
46+
47+
TreeNode* tNode= new TreeNode(postorder[pIndex--]);
48+
49+
if(start==end) return tNode;
50+
51+
int index=mp[tNode->val];
52+
53+
tNode->right=constructTree(postorder, inorder, index+1, end);
54+
tNode->left=constructTree(postorder, inorder, start, index-1);
55+
56+
57+
return tNode;
58+
59+
}
60+
61+
TreeNode* buildTree(vector<int> &inorder, vector<int>& postorder) {
62+
63+
int n=inorder.size();
64+
65+
// Storing data in map for O(1) searching
66+
for(int i=0;i<n;i++)
67+
mp[inorder[i]]=i;
68+
69+
return constructTree(postorder, inorder, 0, n-1); // <postorder, inorder, start, end>
70+
}
71+
};
72+
73+
// Code gives Heap buffer overflow error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
2+
3+
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
4+
5+
Example:
6+
7+
Given the sorted array: [-10,-3,0,5,9],
8+
9+
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
10+
11+
0
12+
/ \
13+
-3 9
14+
/ /
15+
-10 5
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
/**
29+
* Definition for a binary tree node.
30+
* struct TreeNode {
31+
* int val;
32+
* TreeNode *left;
33+
* TreeNode *right;
34+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
35+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
36+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
37+
* };
38+
*/
39+
class Solution {
40+
public:
41+
42+
void convert(vector<int> &nums, int start, int end, TreeNode* &root){
43+
if(start > end) return;
44+
45+
int mid=start+(end-start)/2;
46+
root=new TreeNode(nums[mid]);
47+
48+
convert(nums, start, mid-1, root->left);
49+
convert(nums, mid+1, end, root->right);
50+
}
51+
52+
TreeNode* sortedArrayToBST(vector<int>& nums) {
53+
int n=nums.size();
54+
TreeNode *root=NULL;
55+
convert(nums, 0, n-1, root);
56+
return root;
57+
}
58+
};

0 commit comments

Comments
 (0)