-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
57 lines (53 loc) · 1.71 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//和LeetCode_105和LeetCode_106思路一样。
//切片规则是以找到一个位置,在此位置在pre和post切片的数组中数字相同。
using System;
using System.Linq;
namespace ConstructBinaryTree
{
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;
}
}
class Program
{
static void Main(string[] args)
{
int[] pre = {1, 2, 4, 5, 3, 6, 7};
int[] post = {4, 5, 2, 6, 7, 3, 1};
Console.WriteLine(ConstructFromPrePost(pre, post));
}
static TreeNode ConstructFromPrePost(int[] pre, int[] post)
{
if (pre.Length == 0 || post.Length == 0) return null;
int index = FindIndex(pre[1..], post[..^1]);
var leftPost = post[..(index + 1)];
var rightPost = post[(index + 1)..^1];
var leftPre = pre[1..(index + 2)];
var rightPre = pre[(index + 2)..];
var root = new TreeNode(pre[0]);
root.left = ConstructFromPrePost(leftPre, leftPost);
root.right = ConstructFromPrePost(rightPre, rightPost);
return root;
}
static int FindIndex(int[] pre, int[] post)
{
var record = new int[31];
for (int i = 0; i < post.Length; i++)
{
record[pre[i]]++;
record[post[i]]--;
if (record.All(x => x == 0))
return i;
}
return -1;
}
}
}