AUG!!! DFS&Java&Explaination ^_^ It is good for your health to read this!

  • 1

    Sorry about the last post.. I touched tab by mistake and It posted it. Check this one!

    public class Solution {
            // Two global containers.
            List<List<Integer>> res = new ArrayList<>();
    	List<Integer> curr = new ArrayList<>();
    	public List<List<Integer>> pathSum(TreeNode root, int sum) {
    		if(root == null) return res;
    		curr.add(root.val);//Add the value to the curr.
                    if(root != null && root.left == null&&root.right ==null)
    //if the remaining value is equal to the node value, we got a valid path.
    			if(sum == root.val) 
    				res.add(new ArrayList<Integer>(curr));
    		pathSum(root.left,sum - root.val);// sum -root is the remaining value we need to satisfy
    		pathSum(root.right,sum - root.val);
    		curr.remove(curr.size() - 1);// remember to discard the latest used value.
    		return res;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.