File: root - text - article - 2019 - 12 - reconstrunct-binary-tree-from-preorder-and-inorder-traversals.txt.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 346 Views, 20453 Search Bots | 164 Words
| Browse | Archive
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 346 Views, 20453 Search Bots | 164 Words
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Hi, here's your problem today. This problem was recently asked by Microsoft:
You are given the preorder and inorder traversals of a binary tree in the form of arrays. Write a function that reconstructs the tree represented by these traversals.
Example:
Preorder: [a, b, d, e, c, f, g]
Inorder: [d, b, e, a, f, c, g]
The tree that should be constructed from these traversals is:
Here's a start:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 346 Views, 20453 Search Bots | 164 Words You are given the preorder and inorder traversals of a binary tree in the form of arrays. Write a function that reconstructs the tree represented by these traversals.
Example:
Preorder: [a, b, d, e, c, f, g]
Inorder: [d, b, e, a, f, c, g]
The tree that should be constructed from these traversals is:
a
/ \
b c
/ \ / \
d e f g
Here's a start:
from collections import deque
class Node(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def __str__(self):
q = deque()
q.append(self)
result = ''
while len(q):
n = q.popleft()
result += n.val
if n.left:
q.append(n.left)
if n.right:
q.append(n.right)
return result
def reconstruct(preorder, inorder):
# Fill this in.
tree = reconstruct(['a', 'b', 'd', 'e', 'c', 'f', 'g'],
['d', 'b', 'e', 'a', 'f', 'c', 'g'])
print tree
# abcdefg
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Related Articles
- Generate All IP Addresses
- Daily Interview Problem: Arithmetic Binary Tree
- Daily Interview Problem: Deepest Node in a Binary Tree
- Daily Interview Problem: Trapping Rainwater
- Staying on a Chess Board
- Windows Scripting
- Daily Interview Problem: Word Ordering in a Different Alphabetical Order
- Daily Interview Problem: Spiral Traversal of Grid
- Daily Interview Problem: Largest Product of 3 Elements I
- Daily Interview Problem: Find the k-th Largest Element in a List
©2006~2024 SteakOverCooked - 0.00755 Seconds(s) - 2764.793 KB/s - 36 Online
Memory: 494.2 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
![](/images/top.jpg)
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !