class Node(object): def __init__(self, val): self.value = val self.left = None self.right = None def inorder(root): if root is None: return inorder(root.left) print root.value inorder(root.right) #root = Node(25) #a = Node(15) #b = Node(35) #c = Node(10) #d = Node(20) #e = Node(30) #f = Node(40) #g = Node(12) #c.right = g #a.left = c #a.right = d #b.left = e #b.right = f #root.left = a #root.right = b def post_to_bst(arr, start, end): if start > end: return None if start == end: root = Node(arr[start]) return root root = Node(arr[end]) for idx, i in enumerate(arr): if i > arr[end]: break root.left = post_to_bst(arr, start, idx-1) root.right = post_to_bst(arr, idx, end - 1) return root arr = [30, 40, 35, 25] root = post_to_bst(arr, 0, len(arr) - 1) print root.value print inorder(root)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question