Skip to content

Commit 16c7121

Browse files
Construct Binary Tree from Preorder and Inorder Traversal
1 parent 9179dbc commit 16c7121

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
105. Construct Binary Tree from Preorder and Inorder Traversal
3+
4+
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
5+
6+
7+
8+
Example 1:
9+
10+
11+
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
12+
Output: [3,9,20,null,null,15,7]
13+
14+
15+
Example 2:
16+
17+
Input: preorder = [-1], inorder = [-1]
18+
Output: [-1]
19+
20+
21+
Constraints:
22+
- 1 <= preorder.length <= 3000
23+
- inorder.length == preorder.length
24+
- -3000 <= preorder[i], inorder[i] <= 3000
25+
- preorder and inorder consist of unique values.
26+
- Each value of inorder also appears in preorder.
27+
- preorder is guaranteed to be the preorder traversal of the tree.
28+
- inorder is guaranteed to be the inorder traversal of the tree.
29+
*/
30+
31+
/**
32+
* Definition for a binary tree node.
33+
* struct TreeNode {
34+
* int val;
35+
* TreeNode *left;
36+
* TreeNode *right;
37+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
38+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
39+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
40+
* };
41+
*/
42+
43+
#include <bits/stdc++.h>
44+
using namespace std;
45+
46+
struct TreeNode {
47+
int val;
48+
TreeNode *left;
49+
TreeNode *right;
50+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
51+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
52+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
53+
};
54+
55+
/*
56+
Optimized Approach
57+
Time complexity -> O(n) and Space -> O(n)
58+
where:
59+
- n: number of nodes in a binary tree
60+
- inStart: inorderStart
61+
- inEnd: inorderEnd
62+
- preStart: postorderStart
63+
- preEnd: postorderEnd
64+
*/
65+
class Solution {
66+
TreeNode* constructBinaryTree(vector<int> &inorder, int inStart,int inEnd,vector<int> &preorder, int preStart, int preEnd, map<int,int> &mpp){
67+
if(preStart>preEnd || inStart>inEnd){
68+
return nullptr;
69+
}
70+
TreeNode *root=new TreeNode(preorder[preStart]);
71+
int inRoot=mpp[preorder[preStart]];
72+
int numsLeft=inRoot-inStart;
73+
root->left=constructBinaryTree(inorder,inStart,inRoot-1,preorder,preStart+1,preStart+numsLeft,mpp);
74+
root->right=constructBinaryTree(inorder,inRoot+1,inEnd,preorder,preStart+numsLeft+1,preEnd,mpp);
75+
return root;
76+
}
77+
public:
78+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
79+
if(inorder.size()!=preorder.size()){
80+
return nullptr;
81+
}
82+
map<int,int> mpp;
83+
for(int i=0;i<inorder.size();i++){
84+
mpp[inorder[i]]=i;
85+
}
86+
return constructBinaryTree(inorder,0,inorder.size()-1, preorder,0,preorder.size()-1,mpp);
87+
}
88+
};
89+
90+
/*
91+
1. Question link -- https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
92+
93+
2. Solution link -- https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/solutions/5116112/optimized-approach-with-detailed-explanation-best-c-solution-striver-solution
94+
95+
3. Video link -- https://youtu.be/aZNaLrVebKQ
96+
*/

0 commit comments

Comments
 (0)