Redian新闻
>
借人气问一道Samsung的题
avatar
借人气问一道Samsung的题# JobHunting - 待字闺中
z*o
1
求思路,感谢哈
A binary tree is either an empty tree (represented by an empty pointer) or a
node consisting of a single integer value and two binary trees called the
left subtree and right subtree.
A binary tree T is given. A path in tree T is a non-empty sequence of nodes
of tree T such that each consecutive node in the sequence is a subtree of
its preceding node.
A path P in tree T is given. The amplitude of path P is the maximum
difference between values of nodes on path P. The amplitude of tree T is the
maximum amplitude of paths in T. If tree T is empty, then it does not have
any paths and its amplitude is assumed to be 0.
For example, consider the following tree T consisting of nine nodes:
Sequence [9, 8, 2] is a path in this tree. Sequence [12, 8, 2] is not a path
in this tree. Path [5, 8, 2] has amplitude 6. Paths [9, 8, 2] and [5, 8, 12
] have amplitude 7. The amplitude of the tree is 7, as no path in it has
amplitude greater than [9, 8, 2] or [5, 8, 12].
Assume that the following declarations are given:
class Tree {
public int x;
public Tree l;
public Tree r;
}
Write a function:
class Solution { public int solution(Tree T); }
that, given a tree T consisting of N nodes, returns the amplitude of T.
For example, given tree T shown in the example above, the function should
return 7.
Assume that:
· N is an integer within the range [0..1,000,000];
· each element of tree T is an integer within the range [−1,000,
000,000..1,000,000,000].
Complexity:
· expected worst-case time complexity is O(N);
· expected worst-case space complexity is O(N).
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(Tree T) {
// write your code in Java SE 6
}
}
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。