[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS
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 思路为DFS, 只是append进入stack的时候一并append进入当前的path即可. 1. Constraints
1) can be empty 2. Ideas
DFS T: O(n) S: O(n)
1) edge case
2) stack = [(root, str(root.val))]
3) DFS, if leaf, ans.append(path)
4)return ans 3. Codes
3.1) iterable
class Solution:
def path(self, root):
if not root: return []
ans, stack = [], [(root, str(root.val))]
while stack
node, path = stack.pop()
if not node.left and not node.right:
ans.append(path)
if node.right:
stack.append((node.right, path + "->" + node.right.val))
if node.left:
stack.append((node.left, path + "->" + node.left.val))
return ans
3.2) Recursive
class Solution:
def path(self, root):
def helper(root, path, ans):
path += str(root.val)
if not root.left and not root.right:
ans.append(path)
if root.left:
stack.append((root.left, path + "->", ans))
if root.right:
stack.append((root.right, path + "->", ans))
if not root: return []
ans = []
helper(root, "", ans)
return ans
4.. Test cases
1) empty
2) 1
3)
1
/ \
2 3
\
5
[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS的更多相关文章
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode@ [199] Binary Tree Right Side View (DFS/BFS)
https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [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 ...
随机推荐
- 【大数据系列】hadoop集群的配置
一.hadoop的配置文件分类 1.只读类型的默认文件 core-default.xml hdfs-default.xml mapred-default.xml mapred-que ...
- [Vue warn]: Error in render: "SyntaxError: Unexpected token ' in JSON at position 1"
一,场景: 字符串转对象: var str = "{'bankRate':5,'YINGUO':0}" 二,操作: JSON.parse(str)时候,报错 [Vue warn]: ...
- 如何启动、关闭和设置ubuntu防火墙
如何启动.关闭和设置ubuntu防火墙 引自:http://www.cnblogs.com/jiangyao/archive/2010/05/19/1738909.html 就这句话就够了,下面的可以 ...
- hdu3507 Print Article[斜率优化dp入门题]
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- FtpWebRequest UploadFile返回"The underlying connection was closed: The server committed a protocol violation."解决方法
将FtpWebRequest的KeepAlive设置为true. return Return<Boolean>( new Uri(ftpPath + fileName), request ...
- 安装 SQL SERVER MsiGetProductInfo 无法检索 Product Code 1605错误 解决方案
重装数据库服务器上的SQL SERVER 2008 上遇到了以下问题 标题: SQL Server 安装程序失败. SQL Server 安装程序遇到以下错误: MsiGetProductInfo 无 ...
- iOS - ShareSDK第三方分享(图文和视频)和登录
由于近期工作需要自己抽时间搞了一下第三方分享,这里使用的是shareSDK的第三方,在使用的过程中有一些心得和体会,特在此和大家分享一下~ 1.在经过将近一周时间的开发,终于搞定ios分享了. 2.由 ...
- django之admin设置
Django自带的后台管理是Django明显特色之一,可以让我们快速便捷管理数据.后台管理可以在各个app的admin.py文件中进行控制.以下是我最近摸索总结出比较实用的配置.若你有什么比较好的配置 ...
- DISTINCT 与 GROUP BY 的比较
看了很多文章,这两个SQL语句在不同的数据库上面的实现上可能有相同或有不同,但是应当要明确它们在功能概念上的区别,最终得出结论: GROUP BY 用来使用聚集函数获得值,比如 AVG, MAX, M ...
- 170811、Java获取jdk系统环境变量
package com.rick.utils; /******************************************************** *@Desc: 系统变量属性工具类 ...