input to recursion: pre, in-order, start and end of searching range in in-order
pop the top of pre-order in current level
search the node val in in-order sequence to find the corresponding position
create tree node for this node val and partition the searching range to left and right ranges based on this position
Go to the left and right searching range respectively to find the root of left subtree and root of right subtree
Connect the subtrees to current node and return current node
Time: O(n) for searching node in in-order, O(n) for iterating every node in pre-order. So O(n^2) in total
Space: O(height of tree)
4. Coding
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回构造的TreeNode根节点
def reConstructBinaryTree(self, pre, tin):
# write code here
#1. input: pre: pre-order, tin: in-order
# in-order: flatten tree into list
# pre-order: sequence of node to iterate tree
# 2. idea:
# 1. use recursion tree method
# recursion:
# input: pre, in, start and end of searching region in in-order
# output: node of tree in pre-order
# 1. pop the first element in pre-order
# 2. search this node value and pos in in-order sequence
# 3. construct tree node for this node
# 4. separate the in-order sequence into left, right array
# so that we can goto left, right array to reconstruct the left, right
# subtree
# 5. return current tree node
#
# Time: O(n) for searching node in in-order, O(n) for iterating all node in tree
# so O(n^2)
# Space: O(logn) or O(height) for recursion tree
#
#Test: [1,2,3,4,5,6,7],[3,2,4,1,6,5,7]
# 1
# 2 5
# 3 4 6 7
#
#
if not pre or not tin or len(pre)!= len(tin):
return None
root_node = self.construct_node( pre, tin, 0, len(tin)-1)
return root_node
def construct_node(self, pre, tin, start, end):
if len(pre)==0:
return
if start >end:
# return a None if range is invalid
return None
node_val = pre.pop(0)
pos = None
for i in range(start, end+1):
if tin[i] == node_val:
pos = i
break
if pos == None:
return None
node = TreeNode(node_val)
# goto left tree
left_node = self.construct_node( pre, tin, start, pos-1)
# goto right tree
right_node = self.construct_node( pre, tin, pos+1, end)
node.left = left_node
node.right = right_node
return node