Redian新闻
>
binary tree的最长root leaf path
avatar
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)
avatar
c*t
3
BFS.如果node有parent 指针的话

【在 a********x 的大作中提到】
: 如何求解呢?
avatar
c*t
4
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

【在 f*********i 的大作中提到】
: 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)

avatar
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

avatar
c*a
6
150里面有一题很相近的啊
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。