请检查我的代码,我想让我的同意完成这个问题: 您将获得一个二叉树,其中每个节点都包含一个值。设计算法以获得总和为给定值的所有路径。路径不需要在根或叶子处开始或结束,但它必须沿直线向下。 我的代码:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root the root of binary tree
* @param target an integer
* @return all valid paths
*/
public List<List<Integer>> binaryTreePathSum2(TreeNode root, int target) {
// Write your code here
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return result;
}
int sum = root.val;
helper(root, target, sum, path, result);
return result;
}
private void helper(TreeNode root,
int target,
int sum,
List<Integer> path,
List<List<Integer>> result) {
if (root.val == target) {
path.add(root.val);
result.add(new ArrayList<>(path));
}
if (sum == target) {
path.add(root.val);
result.add(new ArrayList<>(path));
} else if (sum < target) {
path.add(root.val);
if (root.left != null) {
helper(root.left, target, sum + root.left.val, path, result);
}
if (root.right != null) {
helper(root.right, target, sum + root.right.val, path, result);
}
if (root.left == null && root.right == null) {
path.remove(path.size() - 1);
}
} else if (sum > target) {
sum = sum - root.val;
if (root.left != null) {
helper(root.left, target, sum + root.left.val, path, result);
}
if (root.right != null) {
helper(root.right, target, sum + root.right.val, path, result);
}
if (root.left == null && root.right == null) {
path.remove(path.size() - 1);
}
}
}
}
输入 请参见二叉树的表示 {1,2,3,4,#,2},6 产量 [[1,3,2]] 预期 [[1,3,2],[1,2,4]]
此时,我的代码输出是错误的答案,我找不到代码中的问题。
答案 0 :(得分:0)
试试这个。我已修改您的解决方案以解决您引发的问题,以及您可能尚未遇到的另一个问题。第二个问题是:给定一棵树[1,2,3,4,#,2,#,#,#,#,#,1]你当前的算法将找不到额外的解决方案[3,2,1]它找到了初始解[1,3,2]。我还添加了一个println
语句来列出每次调用时helper
的参数,但是你可以删除它,因为它只有助于查看递归的运行方式。我包含的main
方法使用包含原始解决方案找不到的路径的两棵树来测试解决方案:
public class Solution {
/**
* @param root
* the root of binary tree
* @param target
* an integer
* @return all valid paths
*/
public List<List<Integer>> binaryTreePathSum2(TreeNode root, int target) {
// Write your code here
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<Integer>();
if (root == null) {
return result;
}
int sum = 0;
helper(root, target, sum, path, result);
return result;
}
private void helper(TreeNode root, int target, int sum, List<Integer> path, List<List<Integer>> result) {
System.out.print("helper(root=" + root.val);
System.out.print(",target=" + target);
System.out.print(",sum=" + sum);
System.out.print(",path=" + path);
System.out.print(",result=" + result);
System.out.println(")");
sum += root.val; // Update sum so it includes current root
path.add(root.val); // Add current node to the path
// To store nodes removed from the start of the current path
List<Integer> startNodes = new ArrayList<Integer>();
// If we've exceeded the target by adding the current node, then remove
// nodes from the head of the path until the sum is <= the target
while (sum > target) {
// Remove node from start of path
Integer head = path.remove(0);
// Add removed node to startNodes list (to be added back later)
startNodes.add(head);
// Subtract the sum of the head
sum -= head;
}
// If we've found a path that sums up to target
if (sum == target) {
// Add this path to the result
result.add(new ArrayList<>(path));
// Remove the head of this path so we can continue looking for other paths
Integer head = path.remove(0);
// Add removed node startNodes list (to be added back later)
startNodes.add(head);
// Subtract the sum of the head, and continue
sum -= head;
}
// sum < target, so continue finding other nodes
if (root.left!=null) {
helper(root.left,target,sum,path,result);
}
if (root.right != null) {
helper(root.right,target,sum,path,result);
}
// remove current node from path before returning
path.remove(path.size()-1);
// If we had to remove nodes from start of path, add them back now
// add it back again
if (!startNodes.isEmpty()) {
path.addAll(startNodes);
}
}
public static void main(String args[]) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.right.left = new TreeNode(2);
root.right.left.left = new TreeNode(1);
Solution s = new Solution();
List<List<Integer>> result = s.binaryTreePathSum2(root, 6);
for (List<Integer> path : result) {
System.out.println(path.toString());
}
TreeNode root2 = new TreeNode(1);
root2.left = new TreeNode(1);
root2.left.left = new TreeNode(1);
root2.left.left.left = new TreeNode(5);
result = s.binaryTreePathSum2(root2, 6);
for (List<Integer> path : result) {
System.out.println(path.toString());
}
}
}
答案 1 :(得分:0)
我自己得到了灵魂。
public void helper(TreeNode root, int sum, ArrayList<Integer> path, int level, List<List<Integer>> results) {
if (root == null) return;
int tmp = sum;
path.add(root.val);
for (int i = level;i >= 0; i--) {
tmp -= path.get(i);
if (tmp == 0) {
List<Integer> temp = new ArrayList<Integer>();
for (int j = i; j <= level; ++j)
temp.add(path.get(j));
results.add(temp);
}
}
findSum(head.left, sum, path, level + 1, results);
findSum(head.right, sum, path, level + 1, results);
buffer.remove(buffer.size() - 1);
}