由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - construct a binary tree from in-order and level-order trav
相关主题
check if a binary tree is a valid binary search tree问道题,binary tree里有一个有indegree 2
请教一个binary search tree和heap的问题。non recursive binary tree traversal in O(n) time and O(1) space
Amazon queston in cupConstruct Binary Tree from Inorder and Postorder Traversal
一道google题binary tree, sum of 2 nodes == given number
出个题。reconstruct binary tree求LeetCode Binary Tree Level Order Traversal II解法
Leetcode上Binary Tree Level Order Traversal II的疑问LeetCode 更新
求教一个onsite面试题目今天计划做20题
How to turn a binary search tree into a sorted array?啥叫encode/decode binary tree啊?
相关话题的讨论汇总
话题: order话题: tree话题: array话题: sub话题: 60
进入JobHunting版参与讨论
1 (共1页)
x*****0
发帖数: 452
1
I guess we can pass the definition of in-order and level-order traversal. :-
).
For example:
50
/ \
10 60
/ \ / \
5 20 55 70
/ / \
51 65 80
The in-order and level-order traversal of the above tree are:
5, 10, 20, 50, 51, 55, 60, 65, 70, 80
50, 10, 60, 5, 20, 55, 70, 51, 65, 80
My idea:
(1) traversal the level-order array to find out the first element which
appears in the in-order array. We call this element as current root.
(2) find the index of current root in the in-order array. The in-order array
is separated by the index. The left side of the in-order array is the left
sub-tree of the current root and the right side of the in-order array is the
right sub-tree of the current root.
(3) update the in-order array as its left side and then go to step 1.
(4) update the in-order array as its right side and then go to step 2.
Take the above tree as an example.
(1) 5 is the first element appears in the in-order array.
(2) [50 ...60] is the left sub-tree of 5 and [20 ... 80] is the right sub-
tree of 5.
(3) update the in-order array as [50 ... 60]
(1) 10 is the first element appears in [50 10 60].
(2) [50] is the left sub-tree of 10 and [60] is the right sub-tree of 10.
(3) update ...
Can anyone help me verify it?
And really appreciate giving another solution~
Thanks,
Zhong
1 (共1页)
进入JobHunting版参与讨论
相关主题
啥叫encode/decode binary tree啊?出个题。reconstruct binary tree
请教find number of duplicates in a binary search treeLeetcode上Binary Tree Level Order Traversal II的疑问
leetcode tree level by level traversal problem求教一个onsite面试题目
Binary Tree Right Side View这个题的point是什么?How to turn a binary search tree into a sorted array?
check if a binary tree is a valid binary search tree问道题,binary tree里有一个有indegree 2
请教一个binary search tree和heap的问题。non recursive binary tree traversal in O(n) time and O(1) space
Amazon queston in cupConstruct Binary Tree from Inorder and Postorder Traversal
一道google题binary tree, sum of 2 nodes == given number
相关话题的讨论汇总
话题: order话题: tree话题: array话题: sub话题: 60