问题描述:输入一颗二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径,每个元素可以进行加或减的操作,从输的根节点往下一直在叶节点形成一条路径;
public void tranceTree(TreeNode node,LinkedList<Integer>templist,Link<Link<Integer>>res,int sum,int target)
{
if(node==null)
{
if(sum==target){res.add(new Link<Integer>(templist));return;}
else
{
return;
}
}
templist.add(node.left.val);
tranceTree(node.left,templist,res,sum+node.left.val,target);
templist.remove(temlist.size()-1);
templist.add(-node.left.val);
tranceTree(node.left,templist,res,sum-node.left.val,target);
templist.remove(temlist.size()-1);
templist.add(node.right.val);
tranceTree(node.right,templist,res,sum+node.left.val,target);
templist.remove(temlist.size()-1);
templist.add(node.left.val);
tranceTree(node.right,templist,res,sum-node.left.val,target);
templist.remove(temlist.size()-1);
}
public List<List<Integer>> TranceTree(TreeNode root,int target)
{
List<List<Integer>> res= new?List<List<Integer>>();
tranceTree(root,new LinkedList<Integer>(),res,0,target);
???????return res;
}