def binary_tree_from_preorder_inorder(preorder, inorder): if len(preorder) == 0: return None lookup = {d: i for i, d in enumerate(inorder)} seen = {preorder[0]} root = BinaryTreeNode(preorder[0]) current = root for i in range(0, len(preorder) - 1): curr_item = lookup[preorder[i]] next_item = lookup[preorder[i + 1]] if next_item < curr_item: # It is left node = BinaryTreeNode(preorder[i + 1]) node.parent = current current.left = node current = node seen.add(preorder[i + 1]) else: # It is right node = BinaryTreeNode(preorder[i + 1]) node.parent = current current.right = node current = node seen.add(preorder[i + 1]) if len(seen) == len(inorder): break next_item += 1 visit = set(inorder[0:next_item + 1]) if visit.issubset(seen): while True: # If the next parent has also been seen then go up to that if inorder[next_item + 1] in seen: next_item += 1 else: break # Go up until we hit the node with the same value while current.data != inorder[next_item]: current = current.parent return root
nodeO = BinaryTreeNode('O',None,nodeP) nodeN = BinaryTreeNode('N') nodeM = BinaryTreeNode('M') nodeL = BinaryTreeNode('L',None,nodeM) nodeK = BinaryTreeNode('K',nodeL,nodeN) nodeJ = BinaryTreeNode('J',None,nodeK) nodeI = BinaryTreeNode('I',nodeJ,nodeO) nodeH = BinaryTreeNode('H') nodeG = BinaryTreeNode('G',nodeH,None) nodeF = BinaryTreeNode('F',None,nodeG) nodeE = BinaryTreeNode('E') nodeD = BinaryTreeNode('D') nodeC = BinaryTreeNode('C',nodeD,nodeE) nodeB = BinaryTreeNode('B',nodeC,nodeF) nodeA = BinaryTreeNode('A',nodeB,nodeI) nodeB.parent = nodeA nodeC.parent = nodeB nodeD.parent = nodeC nodeE.parent = nodeC nodeF.parent = nodeB nodeG.parent = nodeF nodeH.parent = nodeG nodeI.parent = nodeA nodeJ.parent = nodeI nodeK.parent = nodeJ nodeL.parent = nodeK nodeM.parent = nodeL nodeN.parent = nodeK nodeO.parent = nodeI nodeP.parent = nodeO mySolution = Solution()
next_node = curr.right or curr.parent elif curr.left is prev: result += curr.data, next_node = curr.right or curr.parent else: next_node = curr.parent prev, curr = curr, next_node return result # 3 # 2 5 # 1 4 6 root = BinaryTreeNode(3) root.parent = None assert inorder_traversal(root) == [3] root.left = BinaryTreeNode(2) root.left.parent = root root.left.left = BinaryTreeNode(1) root.left.left.parent = root.left assert inorder_traversal(root) == [1, 2, 3] root.right = BinaryTreeNode(5) root.right.parent = root root.right.left = BinaryTreeNode(4) root.right.left.parent = root.right root.right.right = BinaryTreeNode(6) root.right.right.parent = root.right assert inorder_traversal(root) == [1, 2, 3, 4, 5, 6]
return result if __name__ == "__main__": mySolution = Solution() node7 = BinaryTreeNode('7') node4 = BinaryTreeNode('4') node2 = BinaryTreeNode('2', node7, node4) node6 = BinaryTreeNode('6') node5 = BinaryTreeNode('5', node6, node2) node0 = BinaryTreeNode('0') node8 = BinaryTreeNode('8') node1 = BinaryTreeNode('1', node0, node8) node3 = BinaryTreeNode('3', node5, node1) node1.parent = node3 node5.parent = node3 node6.parent = node5 node2.parent = node5 node7.parent = node2 node4.parent = node2 node0.parent = node1 node8.parent = node1 # Tree tio traverse # 3 # / \ # 5 1 # /\ / \ # 6 2 0 8 # / \