<Tree.PreOrder> DFS 113, 129
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的更多相关文章
- 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 ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal
Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...
- [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 ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
随机推荐
- Java-100天知识进阶-GC算法-知识铺(五)
知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停的来唤醒你记忆深处的知识点. GC算法 1.标记清除算法 优缺点:不需要额外空间,但是遍历空间花费大,而且会产生大量 ...
- 短的 Guid 帮助类
直接贴代码了: /// <summary> /// 短的 Guid 帮助类 /// </summary> public class ShortGuidHelper { #reg ...
- centOS禁止普通用户su到root
1.关于su的相关权限涉及到两个文件,分别为/etc/pam.d/su和/etc/login.defs两个配置文件. 2.禁止普通用户su到root,配置如下: 去除/etc/pam.d/su文件中如 ...
- linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0));
static void Main(string[] args) { int[] arr = { 1, 3, 3, 3, 3, 4, 5, 4, 5, 8, 9, 3 }; //不重复 var uniq ...
- web api 记录部署IIS获取服务器地址的类型
获取服务器地址类型分多种,以下记录 1.HttpContext.Current.Server.MapPath("~/File") 返回的值为 D:\3Project\Code\Mo ...
- c# "As" 与 "Is"效率 (原发布csdn 2017-10-07 11:49:18)
十一长假就要过去了,今年假期没有回家,一个人闲着无聊就在看C#语言规范5.0中文版.昨天看了 is运算符和 as运算符,平时项目中也有用到这两种符号,对于其效率也没有进行比较过,趁着假期有空,先看下效 ...
- 在Visual Studio 2019中开启预览功能
在Visual Studio 2019 菜单 [工具] > [选项] > [环境] 下的预览功能页面焕然一新!我们介绍了预览功能页面,以便您可以轻松找到这些功能并能够控制其启用.新布局提供 ...
- 重温CLR(十七)程序集加载和反射
本章主要讨论在编译时对一个类型一无所知的情况下,如何在运行时发现类型的信息.创建类型的实例以及访问类型的成员.可利用本章讲述的内容创建动态可扩展应用程序. 反射使用的典型场景一般是由一家公司创建宿主应 ...
- eclipse启动tomcat警告 [SetPropertiesRule]{Server/Service/Engine/Host/Context}
解决问题:解决办法是:关闭tomcat,双击eclipse下tomcat服务器,在出来的Tomcat server at localhost页面中找到server options选项,选中其中的选项” ...
- 前端开发JS——快速入门
1.JS的核心标准ECMAScript 组成 ECMAScript------>核心语法标准 DOM------------->对文档节点的操作 ...