Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub

Hi, here’s your problem today. This problem was recently asked by Uber:

Given a postorder traversal for a binary search tree, reconstruct the tree. A postorder traversal is a traversal order where the left child always comes before the right child, and the right child always comes before the parent for all nodes.

Here’s some starter code:

class Node():
  def __init__(self, value, left=None, right=None):
    self.value = value
    self.left = left
    self.right = right

  def __repr__(self):
    return "(" + str(self.value) + ", " + self.left.__repr__() + ", " + self.right.__repr__() + ")"

def create_tree(postorder):
  # Fill this in.

# 2 is the root node, with 1 as the left child and 3 as the right child
print(create_tree([1, 3, 2]))