-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
35 lines (34 loc) · 1.15 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//查找nums中的最大值max及其位置index。以max构建根节点root,再以index分割数组。递归调用创建root的左右子树。
//C#数组切片语法。nums[..index] - 0到index(不包含)的所有元素。nums[(index + 1)..nums.Length] - index + 1到数组尾。
using System;
using System.Linq;
namespace MaximumBinaryTree
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
public TreeNode ConstructMaximumBinaryTree(int[] nums)
{
if (nums.Length == 0)
return null;
int max = nums.Max(num => num);
int index = Array.IndexOf(nums, max);
var root = new TreeNode(max);
int[] left = nums[..index];
int[] right = nums[(index + 1)..nums.Length];
root.left = ConstructMaximumBinaryTree(left);
root.right = ConstructMaximumBinaryTree(right);
return root;
}
}
}