mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
@ -1228,6 +1228,19 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
public TreeNode BuildTree(int[] inorder, int[] postorder)
|
||||
{
|
||||
if (inorder.Length == 0 || postorder.Length == null) return null;
|
||||
int rootValue = postorder.Last();
|
||||
TreeNode root = new TreeNode(rootValue);
|
||||
int delimiterIndex = Array.IndexOf(inorder, rootValue);
|
||||
root.left = BuildTree(inorder.Take(delimiterIndex).ToArray(), postorder.Take(delimiterIndex).ToArray());
|
||||
root.right = BuildTree(inorder.Skip(delimiterIndex + 1).ToArray(), postorder.Skip(delimiterIndex).Take(inorder.Length - delimiterIndex - 1).ToArray());
|
||||
return root;
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -788,6 +788,20 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
public TreeNode MergeTrees(TreeNode root1, TreeNode root2)
|
||||
{
|
||||
if (root1 == null) return root2;
|
||||
if (root2 == null) return root1;
|
||||
|
||||
root1.val += root2.val;
|
||||
root1.left = MergeTrees(root1.left, root2.left);
|
||||
root1.right = MergeTrees(root1.right, root2.right);
|
||||
|
||||
return root1;
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -582,6 +582,21 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```C#
|
||||
public TreeNode ConstructMaximumBinaryTree(int[] nums)
|
||||
{
|
||||
if (nums.Length == 0) return null;
|
||||
int rootValue = nums.Max();
|
||||
TreeNode root = new TreeNode(rootValue);
|
||||
int rootIndex = Array.IndexOf(nums, rootValue);
|
||||
|
||||
root.left = ConstructMaximumBinaryTree(nums.Take(rootIndex).ToArray());
|
||||
root.right = ConstructMaximumBinaryTree(nums.Skip(rootIndex + 1).ToArray());
|
||||
return root;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user