Merge pull request #2346 from eeee0717/master

Update 0106.添加C#版
This commit is contained in:
程序员Carl
2023-11-27 09:52:47 +08:00
committed by GitHub
3 changed files with 42 additions and 0 deletions

View File

@ -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">

View File

@ -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">

View File

@ -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">