都是考查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的更多相关文章

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

  2. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  3. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  4. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  5. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  6. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  7. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  8. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  9. leetcode第一刷_Path Sum II

    在更新上面一道题的时候我就想,是不是另一道打印路径的,果不其然啊. 这样的题非经常见的,做法也非常easy,我是用一个引用的vector来存,满足条件之后直接压入结果集中,当然也能够用数组之类的,都一 ...

随机推荐

  1. [POJ 1385] Lifting the Stone (计算几何)

    题目链接:http://poj.org/problem?id=1385 题目大意:给你一个多边形的点,求重心. 首先,三角形的重心: ( (x1+x2+x3)/3 , (y1+y2+y3)/3 ) 然 ...

  2. WayPoint寻路

    寻路在游戏开发中的重要性,不用多说了.从大型ARPG游戏到小规模体验游戏,都会不同程度的用到寻路功能. 塔防类游戏,战棋类游戏经常用到waypoint(路径)寻路. 下面我们来介绍一下waypoint ...

  3. 使用csc命令进行编译

    ①如果csc不是内外部变量的情况下需要在计算机高级系统设置的环境变量里面下面的Path中将值的后面用分号隔开增加.net framework 4.0的文件路径 ②重新以“管理员身份运行cmd” ③将路 ...

  4. 如果出现isapi和CGI限制的时候

    将isapi和CGI中对应的不允许改为允许即可

  5. OC基础(21)

    Foundation框架介绍 NSString基本概念 字符串读写 字符串比较 字符串搜索 字符串截取 字符串替换 字符串与路径 字符串与基本数据类型转换 *:first-child { margin ...

  6. 《Code Complete》ch.16 控制循环

    WHAT? 反复执行的代码片段(你是第一天学编程吗) WHY? 知道如何使用及何时使用每一种循环是创建高质量软件的一个决定性因素 HOW? 检测位于循环开始/循环结尾 带退出的循环 进入循环 只从一个 ...

  7. 一张图看Goodle Clean设计架构

    之前用一张图分析了Google给出的MVP架构,但是在Google给出的所有案例里面除了基本的MVP架构还有其它几种架构,今天就来分析其中的Clean架构.同样的,网上介绍Clean架构的文章很多,我 ...

  8. 怎样用CODESOFT打印连续的条码标签?

    在实际工作中,经常会用CODESOFT条 码打印软件来实现打印连续的条码标签,将这些标签按递增或递减等方式连续打印.这样设置可大大提高用户的工作效率.实现在CODESOFT 2015打印连续条码标签, ...

  9. Debian的一个命令

    dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理Debian的软件包.下面是它的一些命令解释:1)安装软件命令行:dpkg -i <.deb file name> ...

  10. JS 点击按钮后弹出遮罩层,有关闭按钮

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...