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

Sri Hari: Batch-9/Added Articles#4754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
neetcode-gh merged 3 commits intoneetcode-gh:mainfromSrihari2222:develop_articles
Aug 31, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -142,6 +142,45 @@ class Solution {
}
```

```csharp
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode BuildTree(int[] inorder, int[] postorder) {
if (postorder.Length == 0 || inorder.Length == 0) {
return null;
}

int rootVal = postorder[postorder.Length - 1];
TreeNode root = new TreeNode(rootVal);

int mid = Array.IndexOf(inorder, rootVal);

int[] leftInorder = inorder[..mid];
int[] rightInorder = inorder[(mid + 1)..];

int[] leftPostorder = postorder[..mid];
int[] rightPostorder = postorder[mid..^1];

root.left = BuildTree(leftInorder, leftPostorder);
root.right = BuildTree(rightInorder, rightPostorder);

return root;
}
}
```

::tabs-end

### Time & Space Complexity
Expand DownExpand Up@@ -301,6 +340,47 @@ class Solution {
}
```

```csharp
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
int[] postorder;
Dictionary<int, int> inorderIdx;
int idx;

public TreeNode BuildTree(int[] inorder, int[] postorder) {
this.postorder = postorder;
inorderIdx = new Dictionary<int, int>();
for (int i = 0; i < inorder.Length; i++) {
inorderIdx[inorder[i]] = i;
}
idx = postorder.Length - 1;
return Dfs(0, inorder.Length - 1);
}

private TreeNode Dfs(int l, int r) {
if (l > r) return null;

TreeNode root = new TreeNode(postorder[idx--]);
int i = inorderIdx[root.val];
root.right = Dfs(i + 1, r);
root.left = Dfs(l, i - 1);
return root;
}
}
```

::tabs-end

### Time & Space Complexity
Expand DownExpand Up@@ -468,9 +548,49 @@ class Solution {
}
```

```csharp
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
int postIdx, inIdx;
int[] inorder, postorder;

public TreeNode BuildTree(int[] inorder, int[] postorder) {
this.inorder = inorder;
this.postorder = postorder;
postIdx = inIdx = postorder.Length - 1;
return Dfs(int.MaxValue);
}

private TreeNode Dfs(int limit) {
if (postIdx < 0) return null;
if (inorder[inIdx] == limit) {
inIdx--;
return null;
}

TreeNode root = new TreeNode(postorder[postIdx--]);
root.right = Dfs(root.val);
root.left = Dfs(limit);
return root;
}
}
```

::tabs-end

### Time & Space Complexity

- Time complexity: $O(n)$
- Space complexity: $O(n)$ for recursion stack.
- Space complexity: $O(n)$ for recursion stack.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp