leetcode path-sum-ii(Java)

leetcode题目

path-sum-ii -- newcoder 37
路径总和II -- leetcode 113

题目描述

Given a binary tree and a sum, find all root-to-leaf paths 
where each path's sum equals the given sum.

For example:
Given the below binary tree andsum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1
return

[
   [5,4,11,2],
   [5,8,4,5]
]

思路

 * 1、用深度优先搜索DFS
 * 2、每当DFS搜索到新节点时,都要保存该节点。而且每当找出一条路径之后,
 *    都将这个保存到list的路径保存到最终结果二维list中
 * 3、并且,每当DFS搜索到子节点,发现不是路径和时,返回上一个结点时,
 *    需要把该节点从list中移除

代码

package com.leetcode.tree;

import java.util.ArrayList;

/**
 * 题目:
 * path-sum-ii -- newcoder 37
 * 路径总和II -- leetcode 113
 * 
 * 题目描述:
 * 
Given a binary tree and a sum, find all root-to-leaf paths 
where each path's sum equals the given sum.

For example:
Given the below binary tree andsum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1
return

[
   [5,4,11,2],
   [5,8,4,5]
]
 *
 */
public class PathSumII {

	static class TreeNode {
		 int val;
		 TreeNode left;
		 TreeNode right;
		 TreeNode(int x) { val = x; }
	}
	
	private ArrayList<ArrayList<Integer>> cache = new ArrayList<>();

	/**
	 * 思路:
	 * 1、用深度优先搜索DFS
	 * 2、每当DFS搜索到新节点时,都要保存该节点。而且每当找出一条路径之后,
	 *    都将这个保存到list的路径保存到最终结果二维list中
	 * 3、并且,每当DFS搜索到子节点,发现不是路径和时,返回上一个结点时,
	 *    需要把该节点从list中移除
	 */
    public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
    	if (root == null) {
    		return cache;
    	}
    	
    	findPath(root, new ArrayList<Integer>(), sum);
    	
    	return cache;
    }
	
    private void findPath(TreeNode root, ArrayList<Integer> list, int sum) {
    	if (root == null) {
    		return;
    	}
    	
    	int curVal = root.val;
    	list.add(curVal);
    	
    	if (curVal == sum && root.left == null && root.right == null) {
    		cache.add(new ArrayList<>(list));
    	}
    	
    	findPath(root.left, list, sum - curVal);
    	findPath(root.right, list, sum - curVal);
    	
    	list.remove(list.size()-1);
    }
    
	public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode left = new TreeNode(2);
        TreeNode left_left = new TreeNode(1);
        TreeNode right = new TreeNode(3);
        root.left = left;
        left.left = left_left;
        root.right = right;
        System.out.println(new PathSumII().pathSum(root, 4));
	}

}

相关:
[剑指Offer]二叉树中和为某一值的路径

参考:
[LeetCode] Path Sum II 二叉树路径之和之二(递归、非递归解法)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值