leetcode解题之112 & 113 & 437. Path Sum java版(二叉树路径和)
来源:互联网 发布:如何下载淘宝数据包 编辑:程序博客网 时间:2024/05/21 22:38
112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:Given the below binary tree and
sum = 22
, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
public boolean hasPathSum(TreeNode root, int sum) {if (root == null)return false;return pathCore(root, sum);}private boolean pathCore(TreeNode root, int sum) {if (root != null) {if (sum - root.val == 0 && root.left == null && root.right == null)return true;return pathCore(root.left, sum - root.val) || pathCore(root.right, sum - root.val);}return false;}
113. Path Sum II
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 and
sum = 22
,5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1
return
[ [5,4,11,2], [5,8,4,5]]给定二叉树求所有的路径,等于指定的和
public List<List<Integer>> pathSum(TreeNode root, int sum) {List<List<Integer>> ret = new ArrayList<>();if (root == null)return ret;List<Integer> tmp = new ArrayList<>();pathCore(root, ret, tmp, sum);return ret;}// DFS+回溯private void pathCore(TreeNode root, List<List<Integer>> ret, List<Integer> tmp, int sum) {if (root != null) {if (sum - root.val == 0 && root.left == null && root.right == null) {tmp.add(root.val);ret.add(new ArrayList<>(tmp));tmp.remove(tmp.size() - 1);return;}tmp.add(root.val);pathCore(root.left, ret, tmp, sum - root.val);pathCore(root.right, ret, tmp, sum - root.val);tmp.remove(tmp.size() - 1);}}
437. Path Sum III
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards(traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \3 -2 1Return 3. The paths that sum to 8 are:1. 5 -> 32. 5 -> 2 -> 13. -3 -> 11
先从头结点开始遍历,求得满足条件的路径的个数后,再分别遍历其左右子树,求得满足条件的路径再累加。
用队列层次遍历树的每个节点,再从每个节点开始dfs求出满足条件的路径,将所有的累加就得到结果。
private int count = 0;// 如果sum为3,有多条路径1,1,1,那么这属于不同的结果public int pathSum(TreeNode root, int sum) {if (root == null)return 0;// 层次遍历每个结点,每个结点调用函数pathSumCoreQueue<TreeNode> q = new LinkedList<>();q.offer(root);while (!q.isEmpty()) {TreeNode node = q.poll();pathSumCore(node, sum);if (node.left != null)q.offer(node.left);if (node.right != null)q.offer(node.right);}return count;}private void pathSumCore(TreeNode root, int sum) {if (root != null) {if (sum - root.val == 0) {count++;// 因为后续有可能出现相加等于0的情况,不需要return// return;}pathSumCore(root.left, sum - root.val);pathSumCore(root.right, sum - root.val);}}网上版本:
public int pathSum(TreeNode root, int sum) {if (root == null)return 0;//return dfs(root, sum) + pathSum1(root.left, sum) + pathSum1(root.right, sum);return dfsCore(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);}private int dfsCore(TreeNode root, int sum) {int res = 0;if (root == null)return res;if (root.val == sum)res++;res+=dfsCore(root.left, sum - root.val);res+=dfsCore(root.right, sum - root.val);return res;}第二种dfs
private int dfs(TreeNode root, int sum) {if (root == null)return 0;if (sum == root.val)return 1 + dfs(root.left, 0) + dfs(root.right, 0);return dfs(root.left, sum - root.val) + dfs(root.right, sum - root.val);}
- leetcode解题之112 & 113 & 437. Path Sum java版(二叉树路径和)
- leetcode解题之124. Binary Tree Maximum Path Sum Java版 (二叉树的最大路径和)
- Path Sum 二叉树路径和 @LeetCode
- leetcode解题之57. Binary Tree Paths&129. Sum Root to Leaf Numbers Java版 (二叉树路径)
- Leetcode 437. Path Sum III 路径和3 解题报告
- leetcode解题之62&63. Unique Paths ||64. Minimum Path Sum java版(路径(最短)可达)
- [LeetCode]437. Path Sum III(求二叉树中路径和等于sum的数量)
- LeetCode (12) Path Sum (二叉树路径和判断)
- leetcode之二叉树类之路径和系列-----112/113/124/257/437 path sum(牵扯附加OJ572和OJ100, 子树和子拓扑)
- 【leetcode 二叉树路径和】Path Sum 和 Path Sum II
- 437. Path Sum III(二叉树的路径和之三)
- LeetCode 437. Path Sum III 题解 和固定的二叉树路径数目
- Binary Tree Maximum Path Sum (二叉树路径和的最大值) 【leetcode】
- LeetCode 124. Binary Tree Maximum Path Sum(二叉树最大路径和)
- Leetcode 64. Minimum Path Sum 最小路径和 解题报告
- Leetcode 112. Path Sum 路径和 解题报告
- Leetcode 113. Path Sum II 路径和2 解题报告
- LeetCode OJ 之 Path Sum(求路径和)
- 第三章 变量和表达式
- java 多线程 lock接口 的使用
- Java中的JavaBean类
- python连接redis
- Linux系统操作 (1) ———系统的安装
- leetcode解题之112 & 113 & 437. Path Sum java版(二叉树路径和)
- 搭建Linux环境并软件安装(二)
- 0-1背包问题
- 函数指针、指针函数、typedef等的理解
- 前端知识整理
- JavaMail实现简单邮箱验证——163邮箱
- mysql事务隔离级别为Read uncommitted产生脏读原因
- 【OpenCV学习笔记】三十五、角点检测简介
- python 操作 redis