[leetcode]_Path Sum I && II
都是考查DFS。经典回溯算法,问题在于我对该类型的代码不熟悉,目前以参考别人的代码,然后加上自己的实现为主,通过类似的题目加强理解。
一、给定一棵二叉树,判断是否存在从root到leaf的路径和等于给定值sum,存在返回true,否则返回false。
思路:DFS。
代码:
private boolean ifExist = false;
public boolean hasPathSum(TreeNode root, int sum) {
dfs(root , sum , 0);
return ifExist;
}
public void dfs(TreeNode node , int sum , int tempSum){
if(node == null) return; tempSum += node.val;
if(node.left == null && node.right == null && tempSum == sum){
ifExist = true;
return;
}
dfs(node.left , sum , tempSum);
dfs(node.right , sum , tempSum); }
网络上的DFS代码写的很流畅:参考一下,希望以后能写出这么流畅、舒服的代码。
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false; int leftSum = sum - root.val;
if(leftSum == 0 && root.left == null && root.right == null) return true; boolean left = hasPathSum(root.left , leftSum);
boolean right = hasPathSum(root.right , leftSum);
return left || right;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
二、题目:在上题的基础上,如存在路径,则返回具体路径。
代码:
public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> path = new ArrayList<Integer>();
dfs(root , 0 , sum , path , result);
return result;
} public void dfs(TreeNode node , int tempSum , int sum , ArrayList<Integer> curPath , ArrayList<ArrayList<Integer>> result ){
if(node == null) return; tempSum = tempSum + node.val; if(node.left == null && node.right == null){
if(tempSum == sum){
curPath.add(node.val);
ArrayList<Integer> path = new ArrayList<Integer>(curPath);
//用另一个list来转存一下,以便在后来对curPath的操作不会影响到已经add进result的数据。
result.add(path);
curPath.remove(curPath.size() - 1);
//当前路径被记录后,记得删掉添加到list中的节点,回溯
}
return;
} curPath.add(node.val);
dfs(node.left , tempSum , sum , curPath , result);
dfs(node.right , tempSum , sum , curPath , result);
curPath.remove(curPath.size() - 1);
//该节点的左右节点都已经被访问过了,要删掉在list中该节点的记录,回溯
}
DFS的代码还写的很生硬,需要多加练习。
[leetcode]_Path Sum I && II的更多相关文章
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- leetcode第一刷_Path Sum II
在更新上面一道题的时候我就想,是不是另一道打印路径的,果不其然啊. 这样的题非经常见的,做法也非常easy,我是用一个引用的vector来存,满足条件之后直接压入结果集中,当然也能够用数组之类的,都一 ...
随机推荐
- java小程序实例 闰年
判断闰年. package com.test; import java.util.Scanner; import org.junit.Test; public class TestRunNian { ...
- [ActionScript 3.0] flash如何访问父级或者舞台上的变量、函数等的方法
方法一: 进行类型转换,先将root.parent强制转换为MovieClip类型,再进行使用,如下:MovieClip(root).i.MovieClip(this.parent).i.MovieC ...
- nyoj 95 众数问题
点击打开链接 众数问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数, 多 ...
- Esfog_UnityShader教程_漫反射DiffuseReflection
这篇是系列教程的第三篇,最近工作比较紧,所以这个周六周日就自觉去加了刚回来就打开电脑补上这篇,这个系列的教程我会尽量至少保证一周写一篇的.如果大家看过我的上一篇教程<Esfog_UnitySha ...
- 一个关于发邮件的类,可以模拟发送对smtp服务器或者是本地文件夹
namespace SportsStore.Domain.Concrete { public class EmailSettings { public string MailToAddress = & ...
- [转]整理索引碎片,提升SQL Server速度
数据库表A有十万条记录,查询速度本来还可以,但导入一千条数据后,问题出现了.当选择的数据在原十万条记录之间时,速度还是挺快的:但当选择的数据在这一千条数据之间时,速度变得奇慢. 凭经验,这是索引碎片问 ...
- 如何让 Drupal 使用 Wordpress 形式的编辑代码?
如果你曾有过将 Wordpress 网站迁移到 Drupal 的经验,很可能客户会问的第一件事就是如何为 Drupal 添加编辑代码. Wordpress 中的 Shortcodes 插件让使用者可以 ...
- c# 清空txt文本文件的值
FileStream fs1 = null; try { fs1 = new FileStream(@"C:\db.txt", FileMode.Truncate, FileAcc ...
- iOS调试 LLDB
LLDB是个开源的内置于XCode的具有REPL(read-eval-print-loop)特征的Debugger,其可以安装C++或者Python插件. 常用调试命令: 1.print命 ...
- 洛谷P1930 亚瑟王的宫殿 Camelot
P1930 亚瑟王的宫殿 Camelot 19通过 53提交 题目提供者JOHNKRAM 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 很久以前,亚瑟王和 ...