都是考查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. 查看.netframeword版本

    打开“我的电脑“,在地址栏输入 %systemroot%\Microsoft.NET\Framework

  2. Java如何获取文件编码格式

    1:简单判断是UTF-8或不是UTF-8,因为一般除了UTF-8之外就是GBK,所以就设置默认为GBK.  按照给定的字符集存储文件时,在文件的最开头的三个字节中就有可能存储着编码信息,所以,基本的原 ...

  3. Android Studio 快捷键 主键

    Alt+回车 导入包,自动修正Ctrl+N   查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如get ...

  4. 消除PyCharm中满屏的波浪线

    PyCharm使用了较为严格的PEP8的检查规则,如果代码命名不规范,甚至多出的空格都会被波浪线标识出来,导致整个编辑器里铺满了波浪线,右边的滚动条也全是黄色或灰色的标记线,很是影响编辑. 在网上看了 ...

  5. TesCase-GUI(图形用户界面)测试

    GUI测试是功能测试的一种表现形式.不仅要考虑GUI本身的测试,也要考虑GUI所表现的系统功能的测试.   GUI应具有的要素 1.符合标准和规范 2.直观性 (1)用户界面是否洁净.不唐突.不拥挤? ...

  6. POJ2376_Cleaning Shifts_C++

    题目:http://poj.org/problem?id=2376 英文题强行看不懂,只看的懂输入输出,输入n,m,下接n行每行一个区间两个数左端点 l,有端点 r 给出n个闭区间,求选择最少的区间能 ...

  7. tesseract-orc 合并识别结果

    在实际使用 tesseract-orc 识别库的时候,初次制作的识别库很有可能识别率不太理想,需要后期慢慢补充 本文演示如何将多个修正过的box文件合并成一个识别库. 首先,需要图片样本.tif文件, ...

  8. tomcat下部署可以访问的文件夹

    项目名#文件夹名#文件夹名.xml <Context docBase="D:\文件夹名\r文件夹名"/> 例如: test#zhang#test1.xml <Co ...

  9. 文件系统取证分析(第11章:NTFS概念)

    /* Skogkatt 开始翻译于2015-01-24,仅作为学习研究之用,谢绝转载. 2015-01-31更新MFT entry 属性概念. 2015-02-01翻译完成. 译注:我翻译这本书的这三 ...

  10. 慕课网-安卓工程师初养成-2-11 Java常量

    来源:http://www.imooc.com/code/1256 所谓常量,我们可以理解为是一种特殊的变量,它的值被设定后,在程序运行过程中不允许改变. 语法:final 常量名 = 值; 程序中使 ...