p*t
5 楼
举个先序的例子给你吧
while(current != NULL || !stack.empty()){
if(current != NULL){
stack.push(current);
visit(current);
current = current -> left;
}
else{
current = stack.top();
stack.pop();
current = current -> right;
}
}
中序的其实很类似 访问current的时间变一下而已
stack
【在 W***o 的大作中提到】
: 今天给了一个recursion的解法,然后又让我用stack做,我当时萌了,大家说说stack
: 咋解?
while(current != NULL || !stack.empty()){
if(current != NULL){
stack.push(current);
visit(current);
current = current -> left;
}
else{
current = stack.top();
stack.pop();
current = current -> right;
}
}
中序的其实很类似 访问current的时间变一下而已
stack
【在 W***o 的大作中提到】
: 今天给了一个recursion的解法,然后又让我用stack做,我当时萌了,大家说说stack
: 咋解?
w*a
7 楼
后续可以当先序做,push的时候先左后右,最后reverse一下,也简单
k*a
10 楼
曾经有个牛人告诉我,只要可以递归的,都可以用栈实现
d*n
14 楼
There are several ways to traverse a binary tree:
http://codeanytime.blogspot.com/2014/11/binary-tree-postorder-t
http://codeanytime.blogspot.com/2014/11/binary-tree-postorder-t
【在 p**t 的大作中提到】
: 因为递归的实际实现就是把前一个function call入栈 然后处理调用的新call。。
http://codeanytime.blogspot.com/2014/11/binary-tree-postorder-t
http://codeanytime.blogspot.com/2014/11/binary-tree-postorder-t
【在 p**t 的大作中提到】
: 因为递归的实际实现就是把前一个function call入栈 然后处理调用的新call。。
p*t
15 楼
还有pre-order的。。。
【在 d****n 的大作中提到】
: There are several ways to traverse a binary tree:
: http://codeanytime.blogspot.com/2014/11/binary-tree-postorder-t
: http://codeanytime.blogspot.com/2014/11/binary-tree-postorder-t
【在 d****n 的大作中提到】
: There are several ways to traverse a binary tree:
: http://codeanytime.blogspot.com/2014/11/binary-tree-postorder-t
: http://codeanytime.blogspot.com/2014/11/binary-tree-postorder-t
相关阅读
求问HR说still working through the offer approval process什么意思isMatch("ab", ".*") → true 为什么是true???出道题。perfectPermutation弱问:如果还在申绿卡,能申请需要绿卡的职位吗?Oxford Nanopore 纽约再次招人 (转载)发个跪了的FB onsite赚RPA公司干OPT,B公司帮我抽H1b,这样可行吗?问道FB的题Facebook电面适合全职妈妈的兼职工作有大侠做过 proctorU 的测试吗?湾区华美半导体Spring Symposium: IOT Deep Dive也说内推吧化学PHD毕业后,再读CS的Master,可以申请CS的OPT吗?华工又像到了修铁路那时的生死存亡的关键时刻收到email offer后一般还要等多久?湾区就业这么火爆,为啥加州失业率比全国失业率高那么多?国人面试官下意识里出难题 (转载)Google搞量子计算机的是啥组?regular expression mathinc --Java写竟然超时了/。