由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Amazon coding question
相关主题
List Flattening from book 感觉今天结结实实被烙印阴了
G家onsite面经题目: iterative binary tree post order traversal
问一道二叉树遍历的问题? 谢谢!challenge: 找bug
FLG面经:如何分块pre-order遍历一棵树?Amazon 电面
Interview question::Link nodes at same level in a binary tree 怎么做?
问个问题post order traveral using interationPrint a binary tree in level order but starting from leaf node up to root
谁有较好的iterative后序遍历binary tree的代码?BST 找重复节点数
求问把二叉树的recursive遍历改为stack实现的思路Leetcode Min Stack问题
相关话题的讨论汇总
话题: node话题: curnode话题: right话题: stack话题: root
进入JobHunting版参与讨论
1 (共1页)
d****n
发帖数: 233
1
Do postorder traversal without using recursion.
You can't modify the tree node struct which is as following:
struct Node {
int data;
Node *left;
Node *right;
}
If you want to use stack, use no more than 1 stack.
Any idea?:
B*****t
发帖数: 335
2
贴个代码仅供参考
void postOrder() {
Node *p = root, *visited = root;
stack st;
int i = 0;
while(!st.empty() || p){
while(p){
st.push(p);
p = p->left;
}
p = st.top();
if(p->right == NULL || p->right == visited) {
visit(p->c);
st.pop();
visited = p;
p = NULL;
}
else
p = p->right;
}
}

【在 d****n 的大作中提到】
: Do postorder traversal without using recursion.
: You can't modify the tree node struct which is as following:
: struct Node {
: int data;
: Node *left;
: Node *right;
: }
: If you want to use stack, use no more than 1 stack.
: Any idea?:

d****n
发帖数: 233
3
Nice, saved to my library. Thanks.

【在 B*****t 的大作中提到】
: 贴个代码仅供参考
: void postOrder() {
: Node *p = root, *visited = root;
: stack st;
: int i = 0;
: while(!st.empty() || p){
: while(p){
: st.push(p);
: p = p->left;
: }

m*****g
发帖数: 226
4
不是很明白。高手评价一下:
void postOrderTraversal(node *root)
{

stack st;
st.push(root);
st.push(root);
node *curNode;
node *visited;
while(!st.empty())
{
visited=st.top();
st.pop();
curNode=st.top();
st.pop();
if(!(curNode->left&&curNode->right)) {printf(curNode); st.push(curNode)
; continue;}
if(curNode->left && (curNode->left != visited))
{ st.push(curNode);
if(curNode->right) st.push(curNode->right);
st.push(curNode->left);


【在 d****n 的大作中提到】
: Do postorder traversal without using recursion.
: You can't modify the tree node struct which is as following:
: struct Node {
: int data;
: Node *left;
: Node *right;
: }
: If you want to use stack, use no more than 1 stack.
: Any idea?:

z*j
发帖数: 42
5
说说我的理解:
基本上是在用栈模拟递归的后序遍历过程
对栈顶node, 要判断这个node是回溯过来的,还是刚push到栈顶的
case1:栈顶node左右孩子皆空, 到了叶节点.接下来就要回溯啦.
case2:左孩子回溯回来, 则访问右孩子(2sub cases)
case3:右孩子回溯回来, 则访问栈顶节点, 同时栈顶节点出栈
剩下case 是栈顶不是回溯回来的, 则继续压栈(深度优先搜索)
c*********n
发帖数: 1057
6
我想问下这是面试完以后给布置的么?

【在 d****n 的大作中提到】
: Do postorder traversal without using recursion.
: You can't modify the tree node struct which is as following:
: struct Node {
: int data;
: Node *left;
: Node *right;
: }
: If you want to use stack, use no more than 1 stack.
: Any idea?:

l********s
发帖数: 30
7
我的想法和你类似,但不尽一样:
1. if root is empty, return
2. stack.push(root)
3. let prev_print point to the previous node printed. initially, it points
to a dummy node.
3. while stack is not empty
4. case 1: stack.top is a leaf node. print it.
let prev_print point to stack.top. pop stack.
5. case 2: prev_print is a child of stack.top.
let prev_print point to stack.top. pop stack.
6. case 3: otherwise.
push stack.top's children to stack in right first o

【在 z*j 的大作中提到】
: 说说我的理解:
: 基本上是在用栈模拟递归的后序遍历过程
: 对栈顶node, 要判断这个node是回溯过来的,还是刚push到栈顶的
: case1:栈顶node左右孩子皆空, 到了叶节点.接下来就要回溯啦.
: case2:左孩子回溯回来, 则访问右孩子(2sub cases)
: case3:右孩子回溯回来, 则访问栈顶节点, 同时栈顶节点出栈
: 剩下case 是栈顶不是回溯回来的, 则继续压栈(深度优先搜索)

l****q
发帖数: 177
8
有简单的做法:
p = q = root;
while(p!=0)
{
for( ; p->left! = 0 ; p = p->left)
stack.push(p);
while(p != 0 && ( p->right == 0 || p->right == q))
{
//print p
q = p;
if(stack.empty())
return;
p = stack.pop();
}
stack.push(p);
p = p->right;
}
简化在于两点:叶子不要push in; pop out之后的可以再push in
1 (共1页)
进入JobHunting版参与讨论
相关主题
Leetcode Min Stack问题Interview question::
问几个有关Binary tree的题问个问题post order traveral using interation
请教各位大牛一个K-way merge 的问题谁有较好的iterative后序遍历binary tree的代码?
新鲜fb面经求问把二叉树的recursive遍历改为stack实现的思路
List Flattening from book 感觉今天结结实实被烙印阴了
G家onsite面经题目: iterative binary tree post order traversal
问一道二叉树遍历的问题? 谢谢!challenge: 找bug
FLG面经:如何分块pre-order遍历一棵树?Amazon 电面
相关话题的讨论汇总
话题: node话题: curnode话题: right话题: stack话题: root