高级农民
- 积分
- 1745
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-3-18
- 最后登录
- 1970-1-1
|
1
(a) /
(b) Change the sign when putting in the heap. Change it again when returning value.
2
(a) Sometimes. When you have collisions by chance, you may still get the entry.
(b) Always. HashMap is based on hashing of keys, so changing its value won't affect retrieval.
3- public class SumPaths {
- public void printSumPaths(Node t, int k) {
- if (t != null) {
- sumPathsHelper(t, 0, "", k);
- }
- }
- private void sumPathsHelper(Node n, int sum, String path, int k) {
- sum += n.value;
- path += n.value + " ";
- if (n.left == null && n.right == null) {
- if (sum == k) {
- System.out.println(path);
- }
- return;
- }
- if (n.left == null) {
- sumPathsHelper(n.right, sum, path, k);
- }
- if (n.right == null) {
- sumPathsHelper(n.left, sum, path, k);
- }
- }
- private class Node {
- Node left;
- Node right;
- int value;
- }
- }
复制代码 |
|