Given a binary tree, return all root-to-leaf paths.

Note: A leaf is a node with no children.

Example:

Input:

   1
/ \
2 3
\
5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3

Idea: traverse solution (inorder postorder, preorder can not solve this problem) 1253

dfs is the solution.

Basic structure:

if(node.left != null){
sb.append("->"); sb.append(node.left.val);
traverse(node.left, sb);
sb.setLength(sb.length()-2 - String.valueOf(node.left.val).length()); //****
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//regular trwverse
//1 2 5 3
class Solution {
List<String> res = new ArrayList<>();
public List<String> binaryTreePaths(TreeNode root) {
if(root == null) return res;
traverse(root, (new StringBuilder()).append(root.val));
return res;
}
public void traverse(TreeNode node, StringBuilder sb){
if(node.left == null && node.right==null){
res.add(sb.toString());//append the string
return;
}
//left branch
if(node.left != null){
sb.append("->"); sb.append(node.left.val);
traverse(node.left, sb);
sb.setLength(sb.length()-2 - String.valueOf(node.left.val).length()); //****
}
if(node.right != null){
sb.append("->"); sb.append(node.right.val);
traverse(node.right, sb);
sb.setLength(sb.length()-2 - String.valueOf(node.right.val).length());
}
}
}

Question: can I write it into the stack(non-recursive)?

257. Binary Tree Paths (dfs recurive & stack)的更多相关文章

  1. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  2. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  3. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  4. 257. Binary Tree Paths

    题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...

  5. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  6. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  7. [LeetCode&Python] Problem 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  8. [leetcode]257. Binary Tree Paths二叉树路径

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  9. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. linux输入输出及vim管理

    一.理解系统的输入输出 输入输出系统是计算机重要组成部分,是沟通计算机与外界的桥梁. 二.管理输入输出的符号 1.输出重定向 >                       ##重定向正确输出 ...

  2. VScode中Go的相关插件的安装

    一.安装Go插件失败 使用VScode时,当我们安装完go语言扩展时,新建一个go的源码文件,进行保存时,会提示我们需要安装一些go的扩展插件,可别小看这些插件,这些插件都是非常有用的,比如说自动补全 ...

  3. Ubuntu下安装Tomcate

    1.官网下载安装包 http://tomcat.apache.org/download-80.cgi#8.5.9 2.解压 tar -zxvf apache-tomcat-.tar.gz 3.移动到/ ...

  4. Searching the Web论文阅读

    Searching the Web   (Arvind Arasu etc.) 1. 概述 2000年,23%网页每天更新,.com域内网页40%每天更新.网页生存半衰期是10天.描述方法可用Pois ...

  5. js的事件冒泡

    先来段代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  6. pta08-图7 公路村村通 (30分)

    08-图7 公路村村通   (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N ...

  7. Unity 滚轮实现UGUI ScrollView的缩放

    本文原创,转载请注明出处http://www.cnblogs.com/AdvancePikachu/p/7908754.html 前段时间在做一个类似AnimationCurve的可视化编辑器,其中在 ...

  8. dreamweaver,access2010,数学

    dreamweaver 1,点插入-表格-设置表格. 2,再次修改表格,打开属性修改指标. (修改图片时,也可以选中图片打开对应的属性修改) 设置字体: 1,打开属性-页面属性,弹出操作窗口,设置想改 ...

  9. git分支合并冲突

    合并冲突 如果你在两个不同的分支中,对同一个文件的同一个部分进行了不同的修改,Git 就没法干净的合并它们. 如果你对 #53 问题的修改和有关 hotfix 的修改都涉及到同一个文件的同一处,在合并 ...

  10. node.js运行环境变量配置

    -----Windows cmd---------- 1. echo %PATH%  输出Path环境变量 2.set NODE_ENV=testing 定义环境变量 3.echo %NODE_ENV ...