f*i
2 楼
Iterative inorder, when meet a leaf node, such node together with the nodes
in the stack form a path.
Time O(n)
space O(lgn)
in the stack form a path.
Time O(n)
space O(lgn)
c*t
5 楼
似乎可行,写了个recursion。等牛人写个iteration吧
public LinkedList longestPath(Node root) {
if (root == null)
return new LinkedList();
LinkedList leftPath = longestPath(root.left);
LinkedList rightPath = longestPath(root.right);
if (leftPath.size() >= rightPath.size()) {
leftPath.push(root);
return leftPath;
} else {
rightPath.push(root);
return rightPath;
}
}
【在 c********t 的大作中提到】
: how about using post-order? for each node, discard shorter children path,
: save the longer one.
: time: O(n)
: space: O(length of longest path)
:
: nodes
public LinkedList
if (root == null)
return new LinkedList
LinkedList
LinkedList
if (leftPath.size() >= rightPath.size()) {
leftPath.push(root);
return leftPath;
} else {
rightPath.push(root);
return rightPath;
}
}
【在 c********t 的大作中提到】
: how about using post-order? for each node, discard shorter children path,
: save the longer one.
: time: O(n)
: space: O(length of longest path)
:
: nodes
c*a
6 楼
150里面有一题很相近的啊
相关阅读
[合集] CS博士未遂找工作经历嫁了[合集] 讲讲搞定GOOGLE的经历吧OPT pending 的筒子们,打电话要趁早啊i'll provide FREE career counseling俺也说说面试[合集] master本身就是一种失败[合集] Re: 我对明年那2万个H1-b的担心!希望我是杞人忧天![合集] 明年H1B ADV ,不能再用学校出的ABD申请了吗?Amazon.com电面再帖一遍Amazon Onsite的题我今年的 H-1B申请经历, ABD我这两年来找工作的一点总结报Offer,赚RP (不发对不住人了,我可以买包子不?)Programming Pearls -4I am Positivehow to improve Englishsome answers for noriko分享面试过程,积攒RPCS博士未遂找工作经历