二叉树之路径问题

1.二叉树的所有路径

Leetcode 257;medium;

二叉树的路径问题很常见,递归的思路不难理解,但也需要手写过程熟悉思路!

package tree;

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

public class Main0257二叉树的所有路径 {
	public static void main(String[] args) {
		TreeNode root = new TreeNode(1);
		TreeNode node1 = new TreeNode(2);
		TreeNode node2 = new TreeNode(3);
		TreeNode node3 = new TreeNode(5);
		root.left = node1;
		root.right = node2;
		node1.left = node3;
		List<String> paths = new Solution257().binaryTreePaths(root);
		System.out.println(paths);
	}
}

class Solution257 {
	public List<String> binaryTreePaths(TreeNode root) {
		List<String> list = new ArrayList<>();
		if (root == null)
			return list;
		helper(root, "", list);
		return list;
	}

	public void helper(TreeNode root, String val, List<String> list) {
		if (root.left == null && root.right == null) {
			val += root.val;
			list.add(val);
		}
		if (root.left != null)
			helper(root.left, val + root.val + "->", list);
		if (root.right != null)
			helper(root.right, val + root.val + "->", list);
	}
}

2. 根到叶子结点的数字和

Leetcode 129;medium;

package tree;

//257二叉树的所有路径、129从根节点到叶子结点数字之和
public class Main0129求根到叶子结点数字之和 {
	public static void main(String[] args) {
		TreeNode root = new TreeNode(1);
		TreeNode node1 = new TreeNode(1);
		TreeNode node2 = new TreeNode(1);
		root.left = node1;
		root.right = node2;
		int numbers = new Solution129().sumNumbers(root);
		System.out.println(numbers);
	}
}

class Solution129 {
	int sumNum = 0;

	public int sumNumbers(TreeNode root) {
		if (root == null)
			return 0;
		helper(root, 0);
		return sumNum;
	}

	public void helper(TreeNode root, int sum) {
		if (root.left == null && root.right == null) {
			sumNum += sum + root.val;
		}
		if (root.left != null)
			helper(root.left, sum * 10 + root.val * 10);
		if (root.right != null)
			helper(root.right, sum * 10 + root.val * 10);
	}
}

3.是否包含某路径

Leetcode 112;easy;

判断有没有一条从根到叶子结点的路径和于sum相等,如果有返回true;

package tree;

public class Main0112路径总和 {
	public static void main(String[] args) {
		TreeNode root = new TreeNode(5);
		TreeNode node1 = new TreeNode(4);
		TreeNode node2 = new TreeNode(8);
		TreeNode node3 = new TreeNode(11);
		TreeNode node6 = new TreeNode(7);
		TreeNode node7 = new TreeNode(2);
		root.left = node1;
		root.right = node2;
		node1.left = node3;
		node3.left = node6;
		node3.right = node7;
		boolean b = new Solution112().hasPathSum(root, 22);
		System.out.println(b);
	}
}

class Solution112 {
	public boolean hasPathSum(TreeNode root, int sum) {
		if (root == null)
			return false;

		sum -= root.val;
		if (root.left == null && root.right == null)
			return sum == 0;
		return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
	}
}

4.路径总和Ⅱ

Leetcode 113;medium;

package tree;

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

public class Main0113路径总和Ⅱ {
	public static void main(String[] args) {
		TreeNode root = new TreeNode(5);
		TreeNode node1 = new TreeNode(4);
		TreeNode node2 = new TreeNode(8);
		TreeNode node3 = new TreeNode(11);
		TreeNode node5 = new TreeNode(4);
		TreeNode node7 = new TreeNode(2);
		TreeNode node8 = new TreeNode(5);
		root.left = node1;
		root.right = node2;
		node1.left = node3;
		node2.right = node5;
		node3.right = node7;
		node5.left = node8;
		List<List<Integer>> list = new Solution113().pathSum(root, 22);
		System.out.println(list);
	}
}

class Solution113 {
	List<List<Integer>> output = new ArrayList<>();

	public List<List<Integer>> pathSum(TreeNode root, int sum) {
		helper(root, sum, new ArrayList<>());
		return output;
	}

	public void helper(TreeNode root, int sum, List<Integer> list) {
		if (root == null)
			return;
		list.add(root.val);
		if (root.left == null && root.right == null && sum - root.val == 0)
			output.add(new ArrayList<>(list));
		helper(root.left, sum - root.val, list);
		helper(root.right, sum - root.val, list);
		list.remove(list.size() - 1);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值