由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Career cup 4.9 path sum的答案肯定错了
相关主题
请教一个cracking coding interview书上的问题打印从根到叶子节点所有路径的问题
Cracking上一道题求教[leetcode] Minimum Depth of Binary Tree 我的这个答案说wrong answer,但是我在本地跑就是对的.
Leetcode bst max path-----is this solution correct?借人气问一道Samsung的题
path sum II OJ 超时问题在哪儿啊 kth Node of BST,大家帮忙
请问一道关于binary tree的题今天面的太惨了....
又有leetcode题目来请教了linkedin 电面面经!另求内推!!两道题都答出来了,还是跪了。 请大牛分析原因。
Finding all paths sum up to a given value in 150不对吧?问一个题目
这个题做的对吗?Binary Tree Maximum Path Sum
相关话题的讨论汇总
话题: path话题: treenode话题: int话题: node话题: findsum
进入JobHunting版参与讨论
1 (共1页)
S*******C
发帖数: 822
1
4.9 You are given a binary tree in which each node contains a value. Design
an algorithm to print all paths which sum to a given value. The path does
not need to start or end at the root or a leaf
Page 237
他的答案如下
package Question4_9;
import CtCILibrary.TreeNode;
public class Question {

public static void findSum(TreeNode node, int sum, int[] path, int level
) {
if (node == null) {
return;
}

/* Insert current node into path */
path[level] = node.data;

int t = 0;
for (int i = level; i >= 0; i--){
t += path[i];
if (t == sum) {
print(path, i, level);
}
}

findSum(node.left, sum, path, level + 1);
findSum(node.right, sum, path, level + 1);

/* Remove current node from path. Not strictly necessary, since we
would
* ignore this value, but it's good practice.
*/
path[level] = Integer.MIN_VALUE;
}

public static int depth(TreeNode node) {
if (node == null) {
return 0;
} else {
return 1 + Math.max(depth(node.left), depth(node.right));
}
}

public static void findSum(TreeNode node, int sum) {
int depth = depth(node);
int[] path = new int[depth];
findSum(node, sum, path, 0);
}
private static void print(int[] path, int start, int end) {
for (int i = start; i <= end; i++) {
System.out.print(path[i] + " ");
}
System.out.println();
}
public static void main(String [] args){
TreeNode root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(1);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(8);
root.right.left = new TreeNode(2);
root.right.right = new TreeNode(6);
findSum(root, 8);
}
}
这种解法错在只能求单边的单线段path,不能求倒勾形状的path,而倒钩型path显然是
不能忽略的。
比如下面的例子中如果sum等于9显然是存在path的: 3-5-1
但这种解法根本把他忽略了。Career cup出到第5版了还犯这种幼稚的错误真是让人无
语。
5

3 1

4 8 2 6
这题谁有正确的解法?
s******y
发帖数: 936
2
她的解法对她的题没有错, 你理解成了leetcode 那道题了http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/. 我觉得作者对path的理解是自上向下,不能自下向上。我做leetcode 的时候也是cc150 作者那么理解的。 比如1->2->3 这是个linkedlist path 是1, 2, 3 , 按你的理解是3,2,1 也算path,明显3不能走到2 ,这就是cc150的理解,我觉得。
S*******C
发帖数: 822
3
那应该明说 print all downward paths ...
否则这答案肯定不对

【在 s******y 的大作中提到】
: 她的解法对她的题没有错, 你理解成了leetcode 那道题了http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/. 我觉得作者对path的理解是自上向下,不能自下向上。我做leetcode 的时候也是cc150 作者那么理解的。 比如1->2->3 这是个linkedlist path 是1, 2, 3 , 按你的理解是3,2,1 也算path,明显3不能走到2 ,这就是cc150的理解,我觉得。
1 (共1页)
进入JobHunting版参与讨论
相关主题
Binary Tree Maximum Path Sum请问一道关于binary tree的题
讨论一道LeetCode题:Binary Tree Maximum Path Sum又有leetcode题目来请教了
Print all the paths from root to every leaf 的 iterativeFinding all paths sum up to a given value in 150不对吧?
Tree Question: Longest path from root to a leaf这个题做的对吗?
请教一个cracking coding interview书上的问题打印从根到叶子节点所有路径的问题
Cracking上一道题求教[leetcode] Minimum Depth of Binary Tree 我的这个答案说wrong answer,但是我在本地跑就是对的.
Leetcode bst max path-----is this solution correct?借人气问一道Samsung的题
path sum II OJ 超时问题在哪儿啊 kth Node of BST,大家帮忙
相关话题的讨论汇总
话题: path话题: treenode话题: int话题: node话题: findsum