-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path106-ConstructBinaryTreeFromInorderAndPostorderTraversal.cs
35 lines (30 loc) · 1.46 KB
/
106-ConstructBinaryTreeFromInorderAndPostorderTraversal.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
//-----------------------------------------------------------------------------
// Runtime: 96ms
// Memory Usage: 26.3 MB
// Link: https://leetcode.com/submissions/detail/372492608/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _106_ConstructBinaryTreeFromInorderAndPostorderTraversal
{
public TreeNode BuildTree(int[] inorder, int[] postorder)
{
if (inorder.Length == 0 || postorder.Length == 0) return null;
return BuildTree(inorder, 0, inorder.Length, postorder, 0, postorder.Length);
}
private TreeNode BuildTree(int[] inorder, int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd)
{
var root = new TreeNode(postorder[postorderEnd - 1]);
var inorderIndex = inorderStart;
for (; inorderIndex < inorderEnd; inorderIndex++)
if (inorder[inorderIndex] == postorder[postorderEnd - 1])
break;
var leftLength = inorderIndex - inorderStart;
if (leftLength > 0)
root.left = BuildTree(inorder, inorderStart, inorderIndex, postorder, postorderStart, postorderStart + leftLength);
if (inorderEnd > inorderIndex + 1)
root.right = BuildTree(inorder, inorderIndex + 1, inorderEnd, postorder, postorderStart + leftLength, postorderEnd - 1);
return root;
}
}
}