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. js 数字处理Number()

    //js将数字转换保留2位小数 function toDecimal(x) { var val = Number(x) if (!isNaN(parseFloat(val))) { //toFixed ...

  2. JavaScript中使用ActiveXObject操作本地文件夹的方法

    转载地址    http://www.jb51.net/article/48538.htm 在Windows平台上, js可以调用很多Windows提供的ActivexObject,本文就使用js来实 ...

  3. 3 不用IDE开发groovy

    1       不用IDE开发groovy 1.1  不用IDE开发的方法 可以在IDE中运行Groovy类或者脚本,但是Groovy也提供了其他运行途径.你能运行Groovy代码基于以下: ·    ...

  4. VS2015+NUnit+OpenCover 完成单元测试代码覆盖率测试

    1.VS2015+NUnit+OpenCover 完成单元测试代码覆盖率测试 https://download.csdn.net/download/qq_39441918/10522539 2.*注意 ...

  5. Linux下安装&运行Jmeter程序

    Jmeter在linux系统中运行需要安装jdk和Jmeter两个软件: 1.安装JDK 先检查系统是否有安装jdk,在linux中执行如下命令:java -version  如果返回版本信息,说明系 ...

  6. 多线程编程_读写锁ReadWriteLock

    Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. 读写锁:分为读 ...

  7. fillder script使用

    打开fiddler script editor 在fiddler中Rules -> Customize Rules打开 在editor中点击open, 打开CustomRules.js文件, 对 ...

  8. NodeJS 开发应用

    NodeJS 开发应用 使用的 Node 版本: V8.11.4 开发工具: VSCode 1.27.1 系统: Deepin 15.7 Desktop x64 项目结构 项目结构 Project i ...

  9. 再次梳理css3动画部分知识

    1.transform: 适用于2D或3D转换的元素 transform-origin:元素的位置点 css3转换(2D转换和3D转换):可以对元素进行移动.缩放.转动.拉长或拉伸. 2D转换:tra ...

  10. JS动态创建SVG元素并绑定事件

    var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); svg.set ...