113. Path Sum II

利用DFS的三要素, 出口1,出口2,拆解,记得回溯的时候要回退一位path。

class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
dfs(res, new ArrayList<Integer>(), sum, root);
return res;
} private void dfs(List<List<Integer>> res, List<Integer> path, int sum, TreeNode root){
if(root == null) return;
sum -= root.val;
//leaf node?
if(root.left == null && root.right == null){
if(sum == 0){
path.add(root.val);
res.add(new ArrayList<Integer>(path));
path.remove(path.size() - 1);
}
return;
}
//拆解
path.add(root.val);
dfs(res, path, sum, root.left);
dfs(res, path, sum, root.right);
path.remove(path.size() - 1);
}
}

129. Sum Root to Leaf Numbers

都是利用DFS递归来解,这道题由于不是单纯的把各个节点的数字相加,而是每遇到一个新的子结点的数字,要把父结点的数字扩大10倍之后再相加。如果遍历到叶结点了,就将当前的累加结果sum返回。如果不是,则对其左右子结点分别调用递归函数,将两个结果相加返回即可

class Solution {
public int sumNumbers(TreeNode root) {
return dfs(0, root);
} private int dfs(int sum, TreeNode root){
if(root == null) return 0;
sum = sum * 10 + root.val;
if(root.left == null && root.right == null){
return sum;
}
return dfs(sum, root.left) + dfs(sum, root.right);
}
}

<Tree.PreOrder> DFS 113, 129的更多相关文章

  1. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  2. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  3. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  4. 3月3日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

  5. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  6. LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal

    Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...

  7. [LeetCode] N-ary Tree Preorder Traversal N叉树的前序遍历

    Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary  ...

  8. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  9. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...

随机推荐

  1. VMware虚拟机安装Centos7后设置静态ip

    VMware虚拟机安装Centos7后设置静态ip 1. 先设置虚拟机的网络,打开虚拟网络编辑器: 2. 选择Vm8 234都要勾选 3. 打开NAT设置,看到123,待会要用. 4. 打开虚拟机服务 ...

  2. FFmpeg 是什么?

    笔者才开始学习音视频开发,主要是通过阅读刘歧.赵文杰编著的<FFmpeg从入门到精通>以及雷霄骅博士博客总结写的入门心得体会. 官方文档资料 FFmpeg官方文档:https://ffmp ...

  3. Python 将numpy array由浮点型转换为整型

    Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:

  4. vue 移动端上传图片结合localResizeIMG插件进行图片压缩

    localResizeIMG插件的功能是将图片进行压缩,然后转换成base64传给后台. 首先, npm i lrz -save 然后,再main.js里面引入lrz import lrz from ...

  5. idea配置热加载

    第一步:添加依赖 spring-boot项目中引入如下依赖 <dependency> <groupId>org.springframework.boot</groupId ...

  6. AngleSharp 实战(04)之遍历内部超链接(a)元素的 Href 和 InnerText

    文档地址:https://anglesharp.github.io/docs/Examples.html 直接贴代码了: using System; using System.Linq; using ...

  7. .NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件

    1.添加Microsoft.Office.Interop.Outlook引用 2.封装发送邮件方法 using System; using System.Configuration; using Sy ...

  8. PhantomJS简单使用

    PhantomJS下载地址:   http://phantomjs.org/download.html 简单使用: from selenium import webdriver # 要想调用键盘按键操 ...

  9. STP生成树理解

    1.STP的功能 a. 防止二层环路    b .实现网络冗余备份 2.STP的选择机制 目的:  确定阻塞的端口 STP 交换机的角色: 根交换机,非根交换机 STP的选票:     BPDU Ro ...

  10. css中的行内元素和块级元素总结

    块级元素 <address>,  <button>,  <caption>,  <dd>,  <del>,  <div>,  & ...