-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path106.BuildTree.cs
47 lines (42 loc) · 1.63 KB
/
106.BuildTree.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
// 106. Construct Binary Tree from Inorder and Postorder Traversal
// Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary
// tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.
// Example 1:
// Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
// Output: [3,9,20,null,null,15,7]
// Example 2:
// Input: inorder = [-1], postorder = [-1]
// Output: [-1]
// Constraints:
// 1 <= inorder.length <= 3000
// postorder.length == inorder.length
// -3000 <= inorder[i], postorder[i] <= 3000
// inorder and postorder consist of unique values.
// Each value of postorder also appears in inorder.
// inorder is guaranteed to be the inorder traversal of the tree.
// postorder is guaranteed to be the postorder traversal of the tree.
/**
* 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 BuildTree(int[] inorder, int[] postorder) {
if(postorder is null || !postorder.Any() ||
inorder is null || !inorder.Any())
return null;
TreeNode root = new TreeNode(postorder[^1]);
int middle = Array.IndexOf(inorder, postorder[^1]);
root.left = BuildTree(inorder[..middle], postorder[..middle]);
root.right = BuildTree(inorder[(middle+1)..], postorder[(middle)..^1]);
return root;
}
}