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

Note: A leaf is a node with no children.

Example:

  1. Input:
  2.  
  3. 1
  4. / \
  5. 2 3
  6. \
  7. 5
  8.  
  9. Output: ["1->2->5", "1->3"]
  10.  
  11. 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:

  1. if(node.left != null){
  2. sb.append("->"); sb.append(node.left.val);
  3. traverse(node.left, sb);
  4. sb.setLength(sb.length()-2 - String.valueOf(node.left.val).length()); //****
  5. }
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. //regular trwverse
  11. //1 2 5 3
  12. class Solution {
  13. List<String> res = new ArrayList<>();
  14. public List<String> binaryTreePaths(TreeNode root) {
  15. if(root == null) return res;
  16. traverse(root, (new StringBuilder()).append(root.val));
  17. return res;
  18. }
  19. public void traverse(TreeNode node, StringBuilder sb){
  20. if(node.left == null && node.right==null){
  21. res.add(sb.toString());//append the string
  22. return;
  23. }
  24. //left branch
  25. if(node.left != null){
  26. sb.append("->"); sb.append(node.left.val);
  27. traverse(node.left, sb);
  28. sb.setLength(sb.length()-2 - String.valueOf(node.left.val).length()); //****
  29. }
  30. if(node.right != null){
  31. sb.append("->"); sb.append(node.right.val);
  32. traverse(node.right, sb);
  33. sb.setLength(sb.length()-2 - String.valueOf(node.right.val).length());
  34. }
  35. }
  36. }

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. 毕业设计 python opencv实现车牌识别 界面

    主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...

  2. A. Cinema Line

    A. Cinema Line time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. django组件之form

    form组件 首先要了解form组件的一些基本语法: 1.  校验数据:    form组件校验的是字典,所以数据应该以字典形式传进去    form 校验,可以多传数据,不要紧(只校验form组件有 ...

  4. java/Android String.split 字符串分割

    特殊符号分割时需加[].如下图

  5. 17-----BBS论坛

    BBS论坛(十七) 17.首页导航条实现和代码抽离 (1)temlates/common/_head.html <meta name="csrf-token" content ...

  6. my.ZC

    1.100级,裸身,满技能,属性模拟 数据:   大唐 方寸 化生 龙宫 普陀 地府 狮驼 魔王   气血 1200 1900 2600 1200 2600 2600 1900 1900   魔法 7 ...

  7. 移动测试之appium+python 环境安装(一)

    准备工作 一.Python安装 下载地址 及环境变量配置 注意:安装时候记得勾选上Add python.exe to Path.这可以省略环境变量配置. 如果没有勾选,安装下边操作 找到path环境变 ...

  8. requirej入门nodeTpl使用(三)

    基本语法 HTML部分 在模板中的 HTML 部分,使用定界符“<?”和“?>”作为语法的开始和结束. 在定界符内,可以书写任意JavaScript语句,如: <?for(var i ...

  9. Django-4 模板层

    你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...

  10. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...