由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 转一些我blog上一些常见的二叉树面试问题和总结
相关主题
G电面面经:BT的iterator inorder traversal攒人品,Amazon 二面面经
bloomberg onsite题Lowest common ancestor of two nodes of Binary Tree
这个check whether a binary tree is a BST 问题在版上看到的G题
谁有较好的iterative后序遍历binary tree的代码?recovery BST 不考虑相同值的情况么?
树 inorder下个节点最好办法是啥问道题,binary tree里有一个有indegree 2
binary tree, sum of 2 nodes == given numberLC的BST iterator到底要考察什么?
请教find number of duplicates in a binary search tree什么也不管了,给了一个烙印很差的feedback
F家phone interview的一道题LeetCode 更新
相关话题的讨论汇总
话题: node话题: stack话题: null话题: tree
进入JobHunting版参与讨论
1 (共1页)
i**********e
发帖数: 1145
1
二叉树是面试里常见的问题种类,大家在面试前必须熟悉这一类的问题。以下是我收集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌握好。
Determine if a Binary Tree is a Binary Search Tree
这题很常见,microsoft,amazon, google的面试都有人被问过。这题也是二叉树的好题
,必须得对BST的定义搞清楚。有一个常见的陷阱,就是把current node的value和left
node, right node比较;这是不正确的解法。也有一个很容易想到的brute force解法
,但是每个node会被遍历很多次。正确的优解是 (O(N)解,N=number of nodes)有两
种,面试者必须对这题熟悉。
Binary Search Tree In-Order Traversal Iterative Solution
这题应该是 google 电面经常问的问题吧。我那时候就是google电面没答好这问题,所
以就fail了,超级后悔啊。强烈推荐的问题。In-Order traversal能很轻松地用递
A*********r
发帖数: 564
2
不错,正在复习这一块。。
谢谢分享和总结。。

集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌
握好。
left

【在 i**********e 的大作中提到】
: 二叉树是面试里常见的问题种类,大家在面试前必须熟悉这一类的问题。以下是我收集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌握好。
: Determine if a Binary Tree is a Binary Search Tree
: 这题很常见,microsoft,amazon, google的面试都有人被问过。这题也是二叉树的好题
: ,必须得对BST的定义搞清楚。有一个常见的陷阱,就是把current node的value和left
: node, right node比较;这是不正确的解法。也有一个很容易想到的brute force解法
: ,但是每个node会被遍历很多次。正确的优解是 (O(N)解,N=number of nodes)有两
: 种,面试者必须对这题熟悉。
: Binary Search Tree In-Order Traversal Iterative Solution
: 这题应该是 google 电面经常问的问题吧。我那时候就是google电面没答好这问题,所
: 以就fail了,超级后悔啊。强烈推荐的问题。In-Order traversal能很轻松地用递

i**********e
发帖数: 1145
3
Your idea is correct, using two stacks can solve this question easily. However, there are few bugs in your program. Consider the tree below:
3
/ \
9 20
/ \
15 7
PrintTreeLevelZigZag should print 3 20 9 15 7
the rightToLeft order should be changed when it reaches the end of each
level. In your case, you change the order after each iteration.
Another case you want to consider is what if newNode is NULL? you forgot
to check it. This will crash your program.

from
n*****g
发帖数: 16
4
你说的第一条我post帖子后就发现了,第二条我没有注意到。多谢指正!下面是新的
code。好像两个连续的一样的while有点怪,可以把外面的转为do while。
void PrintTreeLevelZigZag(Node* root)
{
Stack *currentStack = new Stack();
Stack *nextStack = new Stack();
Stack *tempStack;
bool bLeftToRight = true;//the print order of the current level is from
left to right if it is true.
Node *newNode;
if(root)
currentStack->Push(root);
while(currentStack->Peek() != NULL)
{
while(currentStack->Peek() != NULL)
{


【在 i**********e 的大作中提到】
: Your idea is correct, using two stacks can solve this question easily. However, there are few bugs in your program. Consider the tree below:
: 3
: / \
: 9 20
: / \
: 15 7
: PrintTreeLevelZigZag should print 3 20 9 15 7
: the rightToLeft order should be changed when it reaches the end of each
: level. In your case, you change the order after each iteration.
: Another case you want to consider is what if newNode is NULL? you forgot

w*****e
发帖数: 158
5
多谢楼主分享。
i**********e
发帖数: 1145
6
还有一个小bug呢。
一开始我也没看到,但是把你程序执行一下就crash了。
问题就出在这行代码:
while(currentStack->top() != NULL)

from

【在 n*****g 的大作中提到】
: 你说的第一条我post帖子后就发现了,第二条我没有注意到。多谢指正!下面是新的
: code。好像两个连续的一样的while有点怪,可以把外面的转为do while。
: void PrintTreeLevelZigZag(Node* root)
: {
: Stack *currentStack = new Stack();
: Stack *nextStack = new Stack();
: Stack *tempStack;
: bool bLeftToRight = true;//the print order of the current level is from
: left to right if it is true.
: Node *newNode;

s*******t
发帖数: 248
7
常看楼主博客,收获很大,谢谢!

集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌
握好。
left

【在 i**********e 的大作中提到】
: 二叉树是面试里常见的问题种类,大家在面试前必须熟悉这一类的问题。以下是我收集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌握好。
: Determine if a Binary Tree is a Binary Search Tree
: 这题很常见,microsoft,amazon, google的面试都有人被问过。这题也是二叉树的好题
: ,必须得对BST的定义搞清楚。有一个常见的陷阱,就是把current node的value和left
: node, right node比较;这是不正确的解法。也有一个很容易想到的brute force解法
: ,但是每个node会被遍历很多次。正确的优解是 (O(N)解,N=number of nodes)有两
: 种,面试者必须对这题熟悉。
: Binary Search Tree In-Order Traversal Iterative Solution
: 这题应该是 google 电面经常问的问题吧。我那时候就是google电面没答好这问题,所
: 以就fail了,超级后悔啊。强烈推荐的问题。In-Order traversal能很轻松地用递

i**********e
发帖数: 1145
8
谢啦,希望你常来 :)

【在 s*******t 的大作中提到】
: 常看楼主博客,收获很大,谢谢!
:
: 集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌
: 握好。
: left

i**********e
发帖数: 1145
9
nicheng,
你的currentStack->peek()是什么意思呀?
peek()是询问最top element的意思吗?

from

【在 n*****g 的大作中提到】
: 你说的第一条我post帖子后就发现了,第二条我没有注意到。多谢指正!下面是新的
: code。好像两个连续的一样的while有点怪,可以把外面的转为do while。
: void PrintTreeLevelZigZag(Node* root)
: {
: Stack *currentStack = new Stack();
: Stack *nextStack = new Stack();
: Stack *tempStack;
: bool bLeftToRight = true;//the print order of the current level is from
: left to right if it is true.
: Node *newNode;

g******d
发帖数: 511
10
好像不用这么复杂.
push完一层后压一个left or right dummy node.下一层根据这个dummy决定从左到右,
或从右到左. 再push一个反方向的dummy node.

from

【在 n*****g 的大作中提到】
: 你说的第一条我post帖子后就发现了,第二条我没有注意到。多谢指正!下面是新的
: code。好像两个连续的一样的while有点怪,可以把外面的转为do while。
: void PrintTreeLevelZigZag(Node* root)
: {
: Stack *currentStack = new Stack();
: Stack *nextStack = new Stack();
: Stack *tempStack;
: bool bLeftToRight = true;//the print order of the current level is from
: left to right if it is true.
: Node *newNode;

相关主题
binary tree, sum of 2 nodes == given number攒人品,Amazon 二面面经
请教find number of duplicates in a binary search treeLowest common ancestor of two nodes of Binary Tree
F家phone interview的一道题在版上看到的G题
进入JobHunting版参与讨论
n*****g
发帖数: 16
11
ihasleetcode, 抱歉没有来得及回你的帖子!是的,peek()的意思是返回top
element,如果不存在应该返回null。我还没有run 这个code,晚上我来run一下看是不
是会crash。

【在 i**********e 的大作中提到】
: nicheng,
: 你的currentStack->peek()是什么意思呀?
: peek()是询问最top element的意思吗?
:
: from

s****1
发帖数: 135
12
总结的好
b********r
发帖数: 118
13
lz没说zig-zag traverse不能recersive吧
我的java code, run过了
public static void zigZagTraverse(BinaryTreeNode root){
if (root == null) return;
Stack curStack = new Stack();
curStack.push(root);
zigZag(curStack, true);
}

public static void zigZag( Stack nodes, boolean
leftThenRight ){
Stack s = new Stack();
while ( !nodes.isEmpty() ){
BinaryTreeNode node = nodes.pop();
System.out.println(node.data);
if ( leftThenRight ){
if ( node.left != null )
s.push(node.left);
if ( node.right != null )
s.push(node.right);
}
else{
if ( node.right != null )
s.push(node.right);
if ( node.left != null )
s.push(node.left);
}
}
if ( s.size() > 0 ){
zigZag(s, (!leftThenRight));
}
return;
}
b********r
发帖数: 118
14
请问这个是什么意思 没看懂 next right到底是指向谁
Populating Next Right Pointers in Each Node
这题就是把所有node的next right指针指向右边邻居。如果没有邻居,应该指向null。
i**********e
发帖数: 1145
15
This is the original question from my blog:
Given a binary tree
struct Node {
Node* leftChild;
Node* rightChild;
Node* nextRight;
}
Populate the nextRight pointers in each node.
Your task is to initialize all nextRight pointers such that it points to its
right neighbor.

【在 b********r 的大作中提到】
: 请问这个是什么意思 没看懂 next right到底是指向谁
: Populating Next Right Pointers in Each Node
: 这题就是把所有node的next right指针指向右边邻居。如果没有邻居,应该指向null。

b********r
发帖数: 118
16
请问你的blog在哪里啊 thanks!!!
right neighbor就是同一个level里右边的那个node对吧
比如下面这个树
1
2 3
6 7 9 11
1,3和11没有nextRight
2的nextRight是3
6是7, 7是9, 9是11?
貌似是BFT的变种?

its

【在 i**********e 的大作中提到】
: This is the original question from my blog:
: Given a binary tree
: struct Node {
: Node* leftChild;
: Node* rightChild;
: Node* nextRight;
: }
: Populate the nextRight pointers in each node.
: Your task is to initialize all nextRight pointers such that it points to its
: right neighbor.

b********r
发帖数: 118
i**********e
发帖数: 1145
18
对的。
如果没有右邻 应该指向null。
可以用BFT来解,但是BFT需要额外空间。

【在 b********r 的大作中提到】
: 请问你的blog在哪里啊 thanks!!!
: right neighbor就是同一个level里右边的那个node对吧
: 比如下面这个树
: 1
: 2 3
: 6 7 9 11
: 1,3和11没有nextRight
: 2的nextRight是3
: 6是7, 7是9, 9是11?
: 貌似是BFT的变种?

b********r
发帖数: 118
19
bft的确不是最好的 看了你的blog 很有帮助 谢谢!

【在 i**********e 的大作中提到】
: 对的。
: 如果没有右邻 应该指向null。
: 可以用BFT来解,但是BFT需要额外空间。

c******n
发帖数: 710
20
Thanks
相关主题
recovery BST 不考虑相同值的情况么?什么也不管了,给了一个烙印很差的feedback
问道题,binary tree里有一个有indegree 2LeetCode 更新
LC的BST iterator到底要考察什么?leetcode tree level by level traversal problem
进入JobHunting版参与讨论
h******3
发帖数: 351
21
请问能否share
Binary Search Tree In Order Iterative Traversal without using a visited flag
的方法?

集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌
握好。
left

【在 i**********e 的大作中提到】
: 二叉树是面试里常见的问题种类,大家在面试前必须熟悉这一类的问题。以下是我收集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌握好。
: Determine if a Binary Tree is a Binary Search Tree
: 这题很常见,microsoft,amazon, google的面试都有人被问过。这题也是二叉树的好题
: ,必须得对BST的定义搞清楚。有一个常见的陷阱,就是把current node的value和left
: node, right node比较;这是不正确的解法。也有一个很容易想到的brute force解法
: ,但是每个node会被遍历很多次。正确的优解是 (O(N)解,N=number of nodes)有两
: 种,面试者必须对这题熟悉。
: Binary Search Tree In-Order Traversal Iterative Solution
: 这题应该是 google 电面经常问的问题吧。我那时候就是google电面没答好这问题,所
: 以就fail了,超级后悔啊。强烈推荐的问题。In-Order traversal能很轻松地用递

p********7
发帖数: 549
22
给楼主补充几个题吧
BST转为双链表
双链表转为平衡BST
求leaf之间最大距离
由inorder和preorder的序列,重建BST
i**********e
发帖数: 1145
23
不用visited flag也可以实现,就是利用一个current pointer加上一个stack。
首先把current初始化为Root,然后一直往左走,当中把经过的node都push上stack里。
碰到null了,就从stack里pop一个出来指向current,打印current的值,然后把
current指向右孩子,然后重复以上的步骤。
当stack里是空的,也就意识着已经打印完毕。
http://www.ihas1337code.com/2010/04/binary-search-tree-in-order-traversal.html
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

flag

【在 h******3 的大作中提到】
: 请问能否share
: Binary Search Tree In Order Iterative Traversal without using a visited flag
: 的方法?
:
: 集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌
: 握好。
: left

i**********e
发帖数: 1145
24
leaf 之间最大的距离?不是很懂。leaf之间必须是同一个level的吗?如果不是同一个
level的应该怎样?
我知道preorder序列能重建BST。但inorder序列不能重建原来的那个BST吧。是不是
inorder序列建一个平衡的BST?
感谢你的补充题,我会研究研究下。
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 p********7 的大作中提到】
: 给楼主补充几个题吧
: BST转为双链表
: 双链表转为平衡BST
: 求leaf之间最大距离
: 由inorder和preorder的序列,重建BST

n******n
发帖数: 12088
25
what is "right neighbor" in ur problem?

【在 b********r 的大作中提到】
: 请问这个是什么意思 没看懂 next right到底是指向谁
: Populating Next Right Pointers in Each Node
: 这题就是把所有node的next right指针指向右边邻居。如果没有邻居,应该指向null。

i**********e
发帖数: 1145
26
比如下面这个树
1
2 3
6 7 9 11
1,3和11没有nextRight
2的nextRight是3
6是7, 7是9, 9是11.
注:没有nextRight应该把nextRight指向NULL.
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 n******n 的大作中提到】
: what is "right neighbor" in ur problem?
n******n
发帖数: 12088
27
如果把9去掉,7的right是11还是nil?

【在 i**********e 的大作中提到】
: 比如下面这个树
: 1
: 2 3
: 6 7 9 11
: 1,3和11没有nextRight
: 2的nextRight是3
: 6是7, 7是9, 9是11.
: 注:没有nextRight应该把nextRight指向NULL.
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com

i**********e
发帖数: 1145
28
这样的话是nil。
原题是你可以assume是full binary tree。

【在 n******n 的大作中提到】
: 如果把9去掉,7的right是11还是nil?
p********7
发帖数: 549
29
需要inorder和preorder 两个序列才能重构
leaf 之间是任意的,可以说是任意节点之间的距离

【在 i**********e 的大作中提到】
: leaf 之间最大的距离?不是很懂。leaf之间必须是同一个level的吗?如果不是同一个
: level的应该怎样?
: 我知道preorder序列能重建BST。但inorder序列不能重建原来的那个BST吧。是不是
: inorder序列建一个平衡的BST?
: 感谢你的补充题,我会研究研究下。
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com

K******g
发帖数: 1870
30
用一个queue+stack也可以实现
其实用queue就是那种标准的level-order遍历的方法,再加个stack就可以zig-zag了

from

【在 n*****g 的大作中提到】
: 你说的第一条我post帖子后就发现了,第二条我没有注意到。多谢指正!下面是新的
: code。好像两个连续的一样的while有点怪,可以把外面的转为do while。
: void PrintTreeLevelZigZag(Node* root)
: {
: Stack *currentStack = new Stack();
: Stack *nextStack = new Stack();
: Stack *tempStack;
: bool bLeftToRight = true;//the print order of the current level is from
: left to right if it is true.
: Node *newNode;

相关主题
求教一道老题bloomberg onsite题
问几个有关Binary tree的题这个check whether a binary tree is a BST 问题
G电面面经:BT的iterator inorder traversal谁有较好的iterative后序遍历binary tree的代码?
进入JobHunting版参与讨论
s**a
发帖数: 131
31
Thanks.

集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌
握好。
left

【在 i**********e 的大作中提到】
: 二叉树是面试里常见的问题种类,大家在面试前必须熟悉这一类的问题。以下是我收集的一些常见二叉树面试问题(包括我亲身经历的)。多做多练习,相信你一定可以掌握好。
: Determine if a Binary Tree is a Binary Search Tree
: 这题很常见,microsoft,amazon, google的面试都有人被问过。这题也是二叉树的好题
: ,必须得对BST的定义搞清楚。有一个常见的陷阱,就是把current node的value和left
: node, right node比较;这是不正确的解法。也有一个很容易想到的brute force解法
: ,但是每个node会被遍历很多次。正确的优解是 (O(N)解,N=number of nodes)有两
: 种,面试者必须对这题熟悉。
: Binary Search Tree In-Order Traversal Iterative Solution
: 这题应该是 google 电面经常问的问题吧。我那时候就是google电面没答好这问题,所
: 以就fail了,超级后悔啊。强烈推荐的问题。In-Order traversal能很轻松地用递

i**********e
发帖数: 1145
32
更新:
Finding the Maximum Height of a Binary Tree
这题就是寻找二叉树的最深度,利用DFS可以轻松解决。挑战的是:如何写非递归的版
本。有两种解法,一是用BFS,解法比较直接。另一种解法是转换成非递归BFS,方法请
参考In-Order Traversal Iterative Solution.
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
i**********e
发帖数: 1145
33
更新:
添加了两道新题,请参考第一页:
Serialization/Deserialization of Binary Tree
Rebuild Binary Search Tree from Pre-order Traversal
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
K**********n
发帖数: 265
34
GOOD, thanks
i**********e
发帖数: 1145
35
怎么可以用一个queue+stack来打印zig-zag呢?
例如:以下这个例子,怎么打印 0, 1, 2, 3, ..., 14?
___ 0___
/ \
1 2
/ \ / \
6 5 4 3
/ \ / \ / \ / \
7 8 9 10 11 12 13 14
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 K******g 的大作中提到】
: 用一个queue+stack也可以实现
: 其实用queue就是那种标准的level-order遍历的方法,再加个stack就可以zig-zag了
:
: from

i**********e
发帖数: 1145
36
Print Edge Nodes (Boundary) of a Binary Tree
问题是要把树的周围counter-clockwise打印出来。先打印root,然后从上到下打印最
左边的节点,然后从左到右的顺序打印叶子节点,然后从下到上打印最右边的节点。这
题的精粹就在于使用depth-first traversal,一个递归就能搞定。先把树分为两个树
(root的左孩子和右孩子)处理。先处理左树,再处理右树。如果卡在怎么从下到上打
印最右边节点,可以想想post-order traversal。
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
i**********e
发帖数: 1145
37
更新,加了一道新问题总结:
Binary Tree Post-Order Traversal Iterative Solution
这题比起 In-Order Traversal 难多了。是很罕见的面试题,好像只有 amazon 问过这
道题。用 visited flags 好做很多,但是不用 visited flags 还是有可能解出来的。
思路就是利用一个变量储存之前访问的节点。然后在每次循环的时候比较之前节点和
stack 上的节点,这样就可以知道我们在往上还是往下走。如果往上走的话,就能得知
是从左节点还是右节点上来的,这有大大的帮助。另外一个方法是使用两个 stack,解
法很简洁,很巧妙,但是空间复杂度没有一个 stack 的解法少。
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
s*****n
发帖数: 5488
38
不能都放在一个stack里面,最后向外push吗?

【在 i**********e 的大作中提到】
: 更新,加了一道新问题总结:
: Binary Tree Post-Order Traversal Iterative Solution
: 这题比起 In-Order Traversal 难多了。是很罕见的面试题,好像只有 amazon 问过这
: 道题。用 visited flags 好做很多,但是不用 visited flags 还是有可能解出来的。
: 思路就是利用一个变量储存之前访问的节点。然后在每次循环的时候比较之前节点和
: stack 上的节点,这样就可以知道我们在往上还是往下走。如果往上走的话,就能得知
: 是从左节点还是右节点上来的,这有大大的帮助。另外一个方法是使用两个 stack,解
: 法很简洁,很巧妙,但是空间复杂度没有一个 stack 的解法少。
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com

i**********e
发帖数: 1145
39
你可以试试,这道题没那么直接。
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 s*****n 的大作中提到】
: 不能都放在一个stack里面,最后向外push吗?
n********p
发帖数: 708
40
mark~~~~~~~~~~
相关主题
谁有较好的iterative后序遍历binary tree的代码?请教find number of duplicates in a binary search tree
树 inorder下个节点最好办法是啥F家phone interview的一道题
binary tree, sum of 2 nodes == given number攒人品,Amazon 二面面经
进入JobHunting版参与讨论
i**********e
发帖数: 1145
41
更新,一道新面试题总结:
Largest BST in a Binary Tree
要求在树里找最大的 BST subtree。注意,这里指的是 subtree,如果不清楚定义,先
跟面试官确定一下。subtree 在维基百科的定义是指包括树节点和它所有的
descendents。做这题前必须知道怎么才能判断树是不是 BST。这题的巧妙之处在于利
用了 bottom-up 的 Depth-first 遍历来解决所有 top-down 遍历的难处。当然,如果
面试官要求的是 largest BST(不一定是 subtree),那就是另外一套思路了。
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
j***y
发帖数: 2074
42
看天书的飘过,崇拜一下楼主。
l**********3
发帖数: 161
43
inorder遍历树,同时找longest increasing sequence?

【在 i**********e 的大作中提到】
: 更新,一道新面试题总结:
: Largest BST in a Binary Tree
: 要求在树里找最大的 BST subtree。注意,这里指的是 subtree,如果不清楚定义,先
: 跟面试官确定一下。subtree 在维基百科的定义是指包括树节点和它所有的
: descendents。做这题前必须知道怎么才能判断树是不是 BST。这题的巧妙之处在于利
: 用了 bottom-up 的 Depth-first 遍历来解决所有 top-down 遍历的难处。当然,如果
: 面试官要求的是 largest BST(不一定是 subtree),那就是另外一套思路了。
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com

i**********e
发帖数: 1145
44
这是个比较容易掉入的误区。
你可以尝试找找反例。
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 l**********3 的大作中提到】
: inorder遍历树,同时找longest increasing sequence?
j*******a
发帖数: 61
45
will this work?
void f(NODE* r)
{
// define stack1, stack2;
stack1.push(r);
while(!stack1.empty && !stack2.empty)
{
while(!stack1.empty)
{
NODE* n=stack1.pop();
if(n->r) stack2.push(n->r);
if(n->l) stack2.push(n->l);
}
while(!stack2.empty)
{
NODE* n=stack2.pop();
if(n->l) stack1.push(n->l);
if(n->r) stack1.push(n->r);
}
}
}
i**********e
发帖数: 1145
46
longest increasing sequence 的方法来找最大的 BST(不管是 subtree 与否) 是个
容易掉入的误区。
你可以参考一下我贴的图片,这就是其中的反例之一.
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 l**********3 的大作中提到】
: inorder遍历树,同时找longest increasing sequence?
i**********e
发帖数: 1145
47
Could you please explain the idea of your code?
I am not quite sure of your idea.
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 j*******a 的大作中提到】
: will this work?
: void f(NODE* r)
: {
: // define stack1, stack2;
: stack1.push(r);
: while(!stack1.empty && !stack2.empty)
: {
: while(!stack1.empty)
: {
: NODE* n=stack1.pop();

z****t
发帖数: 1090
48
顶,过几天再仔细看看
1 (共1页)
进入JobHunting版参与讨论
相关主题
LeetCode 更新树 inorder下个节点最好办法是啥
leetcode tree level by level traversal problembinary tree, sum of 2 nodes == given number
求教一道老题请教find number of duplicates in a binary search tree
问几个有关Binary tree的题F家phone interview的一道题
G电面面经:BT的iterator inorder traversal攒人品,Amazon 二面面经
bloomberg onsite题Lowest common ancestor of two nodes of Binary Tree
这个check whether a binary tree is a BST 问题在版上看到的G题
谁有较好的iterative后序遍历binary tree的代码?recovery BST 不考虑相同值的情况么?
相关话题的讨论汇总
话题: node话题: stack话题: null话题: tree