MCPcopy Create free account
hub / github.com/EndlessCheng/codeforces-go / buildTree

Function buildTree

leetcode/main.go:511–526  ·  view source on GitHub ↗

LC 106 从中序与后序遍历序列构造二叉树

(inorder []int, postorder []int)

Source from the content-addressed store, hash-verified

509
510// LC 106 从中序与后序遍历序列构造二叉树
511func buildTree(inorder []int, postorder []int) *TreeNode {
512 if len(inorder) == 0 {
513 return nil
514 }
515 rootVal := postorder[len(postorder)-1]
516 for i, v := range inorder {
517 if v == rootVal {
518 return &TreeNode{
519 rootVal,
520 buildTree(inorder[:i], postorder[:i]),
521 buildTree(inorder[i+1:], postorder[i:len(postorder)-1]),
522 }
523 }
524 }
525 panic(1)
526}
527
528// LC 117, O(1) 空间复杂度
529func connect(root *Node) *Node {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected