Skip to content

Commit

Permalink
Time: 3 ms (50.65%), Space: 5.9 MB (50.08%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Dec 13, 2024
1 parent 1844ca4 commit d794235
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func buildTree(preorder []int, inorder []int) *TreeNode {
if len(preorder) == 0 || len(inorder) == 0 {
return nil
}

root := &TreeNode{Val: preorder[0]}
i := slices.Index(inorder, preorder[0])
root.Left = buildTree(preorder[1:1+i], inorder[:i])
root.Right = buildTree(preorder[1+i:], inorder[i+1:])
return root
}

0 comments on commit d794235

Please sign in to comment.