LC 106 从中序与后序遍历序列构造二叉树
(inorder []int, postorder []int)
| 509 | |
| 510 | // LC 106 从中序与后序遍历序列构造二叉树 |
| 511 | func 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) 空间复杂度 |
| 529 | func connect(root *Node) *Node { |
nothing calls this directly
no outgoing calls
no test coverage detected