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 ...
随机推荐
- input只能输入非负数
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 斑马打印机客户端GET和POST,以及后端两种打印方式。
斑马打印机客户端GET和POST,以及后端两种打印方式. 背景环境:打印机安装在客户端外网.当用户登录时,通过ajax取服务器数据,返回打印机命令,然后客户端通过JS发送给斑马打印机. 1.使用Get ...
- 8-----BBS论坛
BBS论坛(八) 8.1.发送邮箱验证码功能 (1)cms/resetemail.html {% from 'common/_macros.html' import static %} {% bloc ...
- 安装eclipse for ee
去官网下载最新版本版本的linux版本的eclipse for ee,下载到Downloads文件夹. 解压文件夹 sudo tar -zxvf eclipse-jee-2018-09-linux-g ...
- 从零开始使用vue-cli搭建一个vue项目及注意事项
一.安装node.js 1.根据电脑的自行下载node.js安装包http://nodejs.cn 2.点击安装,按照正常的的一路点击下去 3.验证安装是否成功,按键win+r,输入cmd打开命令行工 ...
- ctrip-apollo
云端多网卡问题: 参考:https://blog.csdn.net/buyaore_wo/article/details/79847404
- Python开源库
某些情况下,pip install xxx找不到,而且在 官方库 也找不到. 那么 第三方库 就派上用场了.
- spring webapp的配置文件放置在项目外的方法
在web.xml中,填写 <context-param> <param-name>CFG_HOME</param-name> ...
- 卸载3DSMAX
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- is 和 == 区别
== 和 is 的区别 == 比较 比较的是两个值 适用于 列表a = '[1:2]'b = '[1:2]'print(a == b) #True 字典a = '{1,2,3}'b = '{ ...