6.15 路径总和——【LeetCode】

在这里插入图片描述

package com.tree.java;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class seven {
	
	/**
	 * 方法一:递归
	 * 递归的步骤:
	 * 1.确定递归函数的参数和返回值
	 * 2.确定终止条件
	 * 3.确定单层递归的逻辑
	* @Description
	* @author DD
	* @date 2022年2月18日下午5:42:03
	* @param root
	* @param targetSum
	* @return
	 */
	//1.确定递归函数的参数和返回值
	public boolean hasPathSum(TreeNode root, int targetSum) {
		if(root == null) {
			return false;
		}
		targetSum -= root.val;
		//2.确定终止条件
		//叶子节点
		if(root.left == null && root.right == null) {
			return targetSum == 0;//如果到达叶子结点时,targetSum==0 则返回true
		}
		//3.确定单层递归的逻辑
		if(root.left != null) {
			boolean left = hasPathSum(root.left, targetSum);
			if(left) {
				return true;
			}
		}
		if(root.right != null) {
			boolean right = hasPathSum(root.right, targetSum);
			if(right) {
				return true;
			}
		}
		return false;//否则返回false
	}
	
	//递归的简洁方法
	public boolean hasPathSum1(TreeNode root, int targetSum) {
		if(root == null) {
			return false;
		}
		
		//判断叶子结点
		if(root.left == null && root.right == null) {
			return root.val == targetSum;//递归到叶子结点的时候,此时的targetSum已经减去了路径上其它结点的值了
		}
		
		//求两侧分支的路径和
		return hasPathSum1(root.left, targetSum - root.val) || hasPathSum1(root.right, targetSum - root.val);
	}
	
	
	//迭代法
	public boolean hasPathSum2(TreeNode root, int targetSum) {
		if(root == null) {
			return false;
		}
		Stack<TreeNode> stack1 = new Stack<>();
		Stack<Integer> stack2 = new Stack<>();
		stack1.push(root);
		stack2.push(root.val);
		while(!stack1.isEmpty()) {
			int size = stack1.size();
			for(int i = 0; i < size; i++) {
				TreeNode node = stack1.pop();
				int sum = stack2.pop();
				// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
				if(node.left == null && node.right == null && sum == targetSum) {
					return true;
				}
				//右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
				if(node.right != null) {
					stack1.push(node.right);
					stack2.push(sum+node.right.val);
				}
				// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
				if(node.left != null) {
					stack1.push(node.left);
					stack2.push(sum+node.left.val);
				}
			}
		}
		return false;
		
	}
	
}

class TreeNode {
	
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode() {}
	TreeNode(int val) { this.val = val; }
	TreeNode(int val, TreeNode left, TreeNode right) {
		this.val = val;
	    this.left = left;
	    this.right = right;
	}
}

在这里插入图片描述



package com.tree.java;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class seven {
	//法一:递归+回溯
	public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
		List<List<Integer>> res = new ArrayList<>();
		if(root == null) {
			return res;
		}
		List<Integer> path = new LinkedList<>();
		preorderdfs(root, targetSum, res, path);
		return res;
	}

	private void preorderdfs(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
		path.add(root.val);
		//叶子结点
		if(root.left == null && root.right == null) {
			// 找到了和为 targetsum 的路径
			if(targetSum - root.val == 0) {
				res.add(new ArrayList<>(path));
			}
			return;// 如果和不为 targetsum,返回
		}
		
		if(root.left != null) {
			preorderdfs(root.left, targetSum-root.val, res, path);//仔细想想下面的回溯语句什么时候会执行,即遇到了叶子结点递归便到达终点 便可回溯
			path.remove(path.size() - 1);//回溯
		}
		if(root.right != null) {
			preorderdfs(root.right, targetSum - root.val, res, path);
			path.remove(path.size() - 1);//回溯
		}
	}
	
	
	//法二:
	List<List<Integer>> result;
	LinkedList<Integer> path;
	public List<List<Integer>> pathSum1(TreeNode root, int targetSum) {
		result = new LinkedList<>();
		path = new LinkedList<>();
		travesal(root, targetSum);
		return result;
	}

	private void travesal(TreeNode root, int count) {
		if(root == null) {
			return;
		}
		path.offer(root.val);
		count -= root.val;
		if(root.left == null && root.right == null && count == 0) {
			result.add(new LinkedList<>(path));
		}
		travesal(root.left, count);
		travesal(root.right, count);
		path.removeLast();//回溯
	}
	
}

class TreeNode {
	
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode() {}
	TreeNode(int val) { this.val = val; }
	TreeNode(int val, TreeNode left, TreeNode right) {
		this.val = val;
	    this.left = left;
	    this.right = right;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DZSpace

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值