257. Binary Tree Paths (dfs recurive & stack)
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)的更多相关文章
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- 257. Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [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 ...
- [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 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- 毕业设计 python opencv实现车牌识别 界面
主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...
- A. Cinema Line
A. Cinema Line time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- django组件之form
form组件 首先要了解form组件的一些基本语法: 1. 校验数据: form组件校验的是字典,所以数据应该以字典形式传进去 form 校验,可以多传数据,不要紧(只校验form组件有 ...
- java/Android String.split 字符串分割
特殊符号分割时需加[].如下图
- 17-----BBS论坛
BBS论坛(十七) 17.首页导航条实现和代码抽离 (1)temlates/common/_head.html <meta name="csrf-token" content ...
- my.ZC
1.100级,裸身,满技能,属性模拟 数据: 大唐 方寸 化生 龙宫 普陀 地府 狮驼 魔王 气血 1200 1900 2600 1200 2600 2600 1900 1900 魔法 7 ...
- 移动测试之appium+python 环境安装(一)
准备工作 一.Python安装 下载地址 及环境变量配置 注意:安装时候记得勾选上Add python.exe to Path.这可以省略环境变量配置. 如果没有勾选,安装下边操作 找到path环境变 ...
- requirej入门nodeTpl使用(三)
基本语法 HTML部分 在模板中的 HTML 部分,使用定界符“<?”和“?>”作为语法的开始和结束. 在定界符内,可以书写任意JavaScript语句,如: <?for(var i ...
- Django-4 模板层
你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...