Skip to content
Open

Trees-1 #1737

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions constructTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Time Complexity : O(n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach:
# Use preorder to pick root nodes sequentially (root -> left -> right).
# Use a hashmap to store (value : index) for inorder traversal for O(1) lookup.
# Maintain a pointer (pre_idx) to track current root in preorder.
# For each root, find its index in inorder to divide into left and right subtrees.
# Recursively build left subtree first, then right subtree using inorder boundaries.

# 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 = right

class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
inorder_index = {val: i for i, val in enumerate(inorder)}
pre_idx = 0

def helper(left, right):
nonlocal pre_idx

if left > right:
return None

root_val = preorder[pre_idx]
pre_idx += 1

root = TreeNode(root_val)
mid = inorder_index[root_val]

root.left = helper(left, mid - 1)
root.right = helper(mid + 1, right)

return root

return helper(0, len(inorder) - 1)
33 changes: 33 additions & 0 deletions validateBST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Time Complexity : O(n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach:
# Preorder gives the root first. Use a hashmap to store (value : index) for inorder traversal.
# Maintain a pointer for preorder index.
# For each root, find its position in inorder to split into left and right subtrees.
# Recursively build left subtree first, then right subtree.

class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
inorder_index = {val: i for i, val in enumerate(inorder)}
pre_idx = 0

def helper(left, right):
nonlocal pre_idx

if left > right:
return None

root_val = preorder[pre_idx]
pre_idx += 1

root = TreeNode(root_val)
mid = inorder_index[root_val]

root.left = helper(left, mid - 1)
root.right = helper(mid + 1, right)

return root

return helper(0, len(inorder) - 1)