[leetcode]_Path Sum I && II

都是考查DFS。经典回溯算法,问题在于我对该类型的代码不熟悉,目前以参考别人的代码,然后加上自己的实现为主,通过类似的题目加强理解。

一、给定一棵二叉树,判断是否存在从root到leaf的路径和等于给定值sum,存在返回true,否则返回false。

思路:DFS。

代码:


 1     private boolean ifExist = false;
2 public boolean hasPathSum(TreeNode root, int sum) {
3 dfs(root , sum , 0);
4 return ifExist;
5 }
6 public void dfs(TreeNode node , int sum , int tempSum){
7 if(node == null) return;
8
9 tempSum += node.val;
10 if(node.left == null && node.right == null && tempSum == sum){
11 ifExist = true;
12 return;
13 }
14 dfs(node.left , sum , tempSum);
15 dfs(node.right , sum , tempSum);
16
17 }

网络上的DFS代码写的很流畅:参考一下,希望以后能写出这么流畅、舒服的代码。


 1     public boolean hasPathSum(TreeNode root, int sum) {
2 if(root == null) return false;
3
4 int leftSum = sum - root.val;
5 if(leftSum == 0 && root.left == null && root.right == null) return true;
6
7 boolean left = hasPathSum(root.left , leftSum);
8 boolean right = hasPathSum(root.right , leftSum);
9 return left || right;
10 }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

二、题目:在上题的基础上,如存在路径,则返回具体路径。

代码:


 1 public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
2 ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
3
4 ArrayList<Integer> path = new ArrayList<Integer>();
5 dfs(root , 0 , sum , path , result);
6 return result;
7 }
8
9 public void dfs(TreeNode node , int tempSum , int sum , ArrayList<Integer> curPath , ArrayList<ArrayList<Integer>> result ){
10 if(node == null) return;
11
12 tempSum = tempSum + node.val;
13
14 if(node.left == null && node.right == null){
15 if(tempSum == sum){
16 curPath.add(node.val);
17 ArrayList<Integer> path = new ArrayList<Integer>(curPath);
//用另一个list来转存一下,以便在后来对curPath的操作不会影响到已经add进result的数据。
18 result.add(path);
19 curPath.remove(curPath.size() - 1);
20 //当前路径被记录后,记得删掉添加到list中的节点,回溯
21 }
22 return;
23 }
24
25 curPath.add(node.val);
26 dfs(node.left , tempSum , sum , curPath , result);
27 dfs(node.right , tempSum , sum , curPath , result);
28 curPath.remove(curPath.size() - 1);
29 //该节点的左右节点都已经被访问过了,要删掉在list中该节点的记录,回溯
30 }

DFS的代码还写的很生硬,需要多加练习。

时间: 2024-08-24 00:23:22

[leetcode]_Path Sum I && II的相关文章

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:All numbers (includ

Leetcode | Path Sum I &amp;&amp; II

Path Sum I 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

leetcode第一刷_Path Sum II

测试策略:静态测试还是动态测试? [对话场景] 成功发布某个软件版本之后,项目团队召开了项目的经验教训总结大会.在会议期间,项目经理小项和测试经理小测进行了如下的对话: 小项:"小测,我们的项目时间压力很大,测试执行是我们的关键路径,测试团队是否可以在测试执行阶段投入更多的人力和物力?"限定时间和人力资源同等条件. 小测:"啊!假如增加我们的测试执行时间,在整个周期不变的情况下,我们就需要压缩前期的学习和评审投入的时间和工作量,是吗?" 小项:"是的,你看

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

LeetCode——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] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于

LeetCode: Path Sum II [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 and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [题意] 判断二叉树中是否存在一条从根到

leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

1.  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 r

Leetcode:Path Sum 二叉树路径和

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

Leetcode:Combination Sum 子集和问题

Combination Sum: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includ