You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclassSolution:
defbuildTree(self, preorder: List[int], inorder: List[int]) ->Optional[TreeNode]:
ifnotpreorderornotinorder:
returnNone# Preorder traversal -> NODE, LEFT, RIGHT# This means that the first element of the `preorder` array will always be the root noderoot=TreeNode(preorder[0])
# Find the index of the root value in the `inorder` array# Inorder traversal -> LEFT, NODE, RIGHT# Everything BEFORE the `nodeIndex` is the inorder left subtree# Everything AFTER the `nodeIndex` is the inorder right subtreenodeIndex=inorder.index(root.val)
# Preorder - element 0 was the root node. Everything after that up to AND INCLUDING the `nodeIndex` is the left subtree# Inorder - Everything from the start of the array up to the `nodeIndex` is the left subtreeroot.left=self.buildTree(preorder[1 : nodeIndex+1], inorder[:nodeIndex])
# Preorder - Everything after the `nodeIndex` to the end of the array is the right subtree# Inorder - Everything after the `nodeIndex` to the end of the array is the right subtreeroot.right=self.buildTree(preorder[nodeIndex+1:], inorder[nodeIndex+1:])
# Return the tree that was buildreturnroot
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: