add new solutions
This commit is contained in:
parent
0c73608ce5
commit
d798d5e8c9
19 changed files with 661 additions and 4 deletions
26
solutions/1/q106/solution.go
Normal file
26
solutions/1/q106/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q106
|
||||
|
||||
import "slices"
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func buildTree(inorder []int, postorder []int) *TreeNode {
|
||||
if len(inorder) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
val := postorder[len(postorder)-1]
|
||||
pos := slices.Index(inorder, val)
|
||||
|
||||
return &TreeNode{
|
||||
Val: val,
|
||||
Left: buildTree(inorder[0:pos], postorder[:pos]),
|
||||
Right: buildTree(inorder[pos+1:], postorder[pos:len(postorder)-1]),
|
||||
}
|
||||
}
|
||||
|
||||
var _ = buildTree
|
||||
Loading…
Add table
Add a link
Reference in a new issue