Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfbbe45c

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 parenta406031 commitfbbe45c

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+
publicclassSolution {
160+
publicTreeNodeBuildTree(int[]inorder,int[]postorder) {
161+
if (postorder.Length==0||inorder.Length==0) {
162+
returnnull;
163+
}
164+
165+
introotVal=postorder[postorder.Length-1];
166+
TreeNoderoot=newTreeNode(rootVal);
167+
168+
intmid=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+
returnroot;
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+
publicclassSolution {
358+
int[]postorder;
359+
Dictionary<int,int>inorderIdx;
360+
intidx;
361+
362+
publicTreeNodeBuildTree(int[]inorder,int[]postorder) {
363+
this.postorder=postorder;
364+
inorderIdx=newDictionary<int,int>();
365+
for (inti=0;i<inorder.Length;i++) {
366+
inorderIdx[inorder[i]]=i;
367+
}
368+
idx=postorder.Length-1;
369+
returnDfs(0,inorder.Length-1);
370+
}
371+
372+
privateTreeNodeDfs(intl,intr) {
373+
if (l>r)returnnull;
374+
375+
TreeNoderoot=newTreeNode(postorder[idx--]);
376+
inti=inorderIdx[root.val];
377+
root.right=Dfs(i+1,r);
378+
root.left=Dfs(l,i-1);
379+
returnroot;
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+
publicclassSolution {
566+
intpostIdx,inIdx;
567+
int[]inorder,postorder;
568+
569+
publicTreeNodeBuildTree(int[]inorder,int[]postorder) {
570+
this.inorder=inorder;
571+
this.postorder=postorder;
572+
postIdx=inIdx=postorder.Length-1;
573+
returnDfs(int.MaxValue);
574+
}
575+
576+
privateTreeNodeDfs(intlimit) {
577+
if (postIdx<0)returnnull;
578+
if (inorder[inIdx]==limit) {
579+
inIdx--;
580+
returnnull;
581+
}
582+
583+
TreeNoderoot=newTreeNode(postorder[postIdx--]);
584+
root.right=Dfs(root.val);
585+
root.left=Dfs(limit);
586+
returnroot;
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)

[8]ページ先頭

©2009-2025 Movatter.jp