-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathleetcode0106.go
41 lines (36 loc) · 1.05 KB
/
leetcode0106.go
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
/*
LeetCode 106: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
*/
package leetcode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// Note: The function name should be buildTree. Rename it to avoid name conflict.
func buildTree106(inorder []int, postorder []int) *TreeNode {
return helper106(inorder, postorder, 0, len(inorder)-1, 0, len(postorder)-1)
}
func helper106(inorder, postorder []int, inStart, inEnd, postStart, postEnd int) *TreeNode {
if inStart > inEnd {
return nil
}
root := &TreeNode{Val: postorder[postEnd]}
rootInorderIndex := inStart
for ; rootInorderIndex <= inEnd; rootInorderIndex++ {
if inorder[rootInorderIndex] == root.Val {
break
}
}
leftLen := rootInorderIndex - inStart
root.Left = helper106(inorder, postorder,
inStart, rootInorderIndex-1,
postStart, postStart+leftLen-1)
root.Right = helper106(inorder, postorder,
rootInorderIndex+1, inEnd,
postStart+leftLen, postEnd-1)
return root
}