-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
27 lines (26 loc) · 909 Bytes
/
Solution.cs
File metadata and controls
27 lines (26 loc) · 909 Bytes
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
/**
* 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 ConstructFromPrePost(int[] preorder, int[] postorder)
{
if (preorder.Length != postorder.Length || preorder.Length == 0) return null;
TreeNode root = new TreeNode(preorder[0]);
if (preorder.Length == 1) return root;
int leftSub = Array.IndexOf(postorder, preorder[1]);
root.left = ConstructFromPrePost(preorder[1..(leftSub + 2)], postorder[..(leftSub + 1)]);
root.right = ConstructFromPrePost(preorder[(leftSub + 2)..], postorder[(leftSub + 1)..^1]);
return root;
}
}