Skip to content

Commit fbbe45c

Browse files
authored
Sri Hari: Batch-9/Added Articles (#4754)
* Batch-9/Neetcode-ALL/Added-articles * Batch-9/Neetcode-ALL/Added-articles * Batch-9/Neetcode-ALL/Added-articles
1 parent a406031 commit fbbe45c

15 files changed

+3161
-32
lines changed

articles/construct-binary-tree-from-inorder-and-postorder-traversal.md

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,45 @@ class Solution {
142142
}
143143
```
144144

145+
```csharp
146+
/**
147+
* Definition for a binary tree node.
148+
* public class TreeNode {
149+
* public int val;
150+
* public TreeNode left;
151+
* public TreeNode right;
152+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
153+
* this.val = val;
154+
* this.left = left;
155+
* this.right = right;
156+
* }
157+
* }
158+
*/
159+
public class Solution {
160+
public TreeNode BuildTree(int[] inorder, int[] postorder) {
161+
if (postorder.Length == 0 || inorder.Length == 0) {
162+
return null;
163+
}
164+
165+
int rootVal = postorder[postorder.Length - 1];
166+
TreeNode root = new TreeNode(rootVal);
167+
168+
int mid = Array.IndexOf(inorder, rootVal);
169+
170+
int[] leftInorder = inorder[..mid];
171+
int[] rightInorder = inorder[(mid + 1)..];
172+
173+
int[] leftPostorder = postorder[..mid];
174+
int[] rightPostorder = postorder[mid..^1];
175+
176+
root.left = BuildTree(leftInorder, leftPostorder);
177+
root.right = BuildTree(rightInorder, rightPostorder);
178+
179+
return root;
180+
}
181+
}
182+
```
183+
145184
::tabs-end
146185

147186
### Time & Space Complexity
@@ -301,6 +340,47 @@ class Solution {
301340
}
302341
```
303342

343+
```csharp
344+
/**
345+
* Definition for a binary tree node.
346+
* public class TreeNode {
347+
* public int val;
348+
* public TreeNode left;
349+
* public TreeNode right;
350+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
351+
* this.val = val;
352+
* this.left = left;
353+
* this.right = right;
354+
* }
355+
* }
356+
*/
357+
public class Solution {
358+
int[] postorder;
359+
Dictionary<int, int> inorderIdx;
360+
int idx;
361+
362+
public TreeNode BuildTree(int[] inorder, int[] postorder) {
363+
this.postorder = postorder;
364+
inorderIdx = new Dictionary<int, int>();
365+
for (int i = 0; i < inorder.Length; i++) {
366+
inorderIdx[inorder[i]] = i;
367+
}
368+
idx = postorder.Length - 1;
369+
return Dfs(0, inorder.Length - 1);
370+
}
371+
372+
private TreeNode Dfs(int l, int r) {
373+
if (l > r) return null;
374+
375+
TreeNode root = new TreeNode(postorder[idx--]);
376+
int i = inorderIdx[root.val];
377+
root.right = Dfs(i + 1, r);
378+
root.left = Dfs(l, i - 1);
379+
return root;
380+
}
381+
}
382+
```
383+
304384
::tabs-end
305385

306386
### Time & Space Complexity
@@ -468,9 +548,49 @@ class Solution {
468548
}
469549
```
470550

551+
```csharp
552+
/**
553+
* Definition for a binary tree node.
554+
* public class TreeNode {
555+
* public int val;
556+
* public TreeNode left;
557+
* public TreeNode right;
558+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
559+
* this.val = val;
560+
* this.left = left;
561+
* this.right = right;
562+
* }
563+
* }
564+
*/
565+
public class Solution {
566+
int postIdx, inIdx;
567+
int[] inorder, postorder;
568+
569+
public TreeNode BuildTree(int[] inorder, int[] postorder) {
570+
this.inorder = inorder;
571+
this.postorder = postorder;
572+
postIdx = inIdx = postorder.Length - 1;
573+
return Dfs(int.MaxValue);
574+
}
575+
576+
private TreeNode Dfs(int limit) {
577+
if (postIdx < 0) return null;
578+
if (inorder[inIdx] == limit) {
579+
inIdx--;
580+
return null;
581+
}
582+
583+
TreeNode root = new TreeNode(postorder[postIdx--]);
584+
root.right = Dfs(root.val);
585+
root.left = Dfs(limit);
586+
return root;
587+
}
588+
}
589+
```
590+
471591
::tabs-end
472592

473593
### Time & Space Complexity
474594

475595
- Time complexity: $O(n)$
476-
- Space complexity: $O(n)$ for recursion stack.
596+
- Space complexity: $O(n)$ for recursion stack.

0 commit comments

Comments
 (0)