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. HDU计算机学院大学生程序设计竞赛(2015’12)Happy Value

    Problem Description In an apartment, there are N residents. The Internet Service Provider (ISP) want ...

  2. 惠普台式机在UEFI BIOS设置通电自动开机 影响电脑自动重启关不了机设置

    设置通电自动开机 影响电脑自动重启关不了机设置   惠普台式机在UEFI BIOS中 1. 开机时不断点击F10键进入BIOS,选择Advanced(高级)然后选择Boot Options,点击回车 ...

  3. set与map

    set /*set里面没有相同的元素 所以可以用于数组去重*/ //内部去重用的是=== 对象不等 但是 NaN等 //其本身是构造函数 let s=new Set([1,2,{},{},3,4,Na ...

  4. spring整合jpa

    有点类似SSH整合,O(∩_∩)O哈哈~ <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  5. Python3 PIL Image 操作文件后导致占用问题

    需求:按图片分辨率范围分类图片(每个商品有N张图片在一个目录内). 实现:用PIL Image打开图片并读取宽度和高度.然后按不同分辨率存档到不同目录. 问题:从原有目录移动到目标目录时出现 “Per ...

  6. axios拦截器搭配token使用

    在了解到cookie.session.token的作用后学习token的使用 cookie是随着url将参数发送到后台,安全性最低,并且大小受限,不超过4kb左右,它的数据保存在客户端 session ...

  7. 5W1H

    WHY   ——为什么做?原因与目标 WHAT ——做什么?目标与内容 WHO   ——谁去做?具体的执行者 WHEN ——什么时间做?执行时间 WHERE——在什么地方做?执行地点 HOW——怎样做 ...

  8. IBM-内存管理内幕 动态分配的选择、折衷和实现

    https://www.ibm.com/developerworks/cn/linux/l-memory/ 为什么必须管理内存 内存管理是计算机编程最为基本的领域之一.在很多脚本语言中,您不必担心内存 ...

  9. 二叉排序树思想及C语言实现

    转自: http://blog.chinaunix.net/uid-22663647-id-1771796.html 1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找( ...

  10. PHP保存Base64图片base64_decode的问题

    PHP对Base64的支持非常好,有内置的base64_encode与base64_decode负责图片的Base64编码与解码. 编码上,只要将图片流读取到,而后使用base64_encode进行进 ...