【LEETCODE】257-Binary Tree Paths
来源:互联网 发布:福建医科大学网络教育 编辑:程序博客网 时间:2024/05/22 22:40
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ self.ans = [] if root is None: return self.ans def dfs(root, path): #if root is None: #外函数已经判断root是否为空了,所以dfs不用再考虑root为空的时候 #return path if root.left == root.right == None: self.ans += path, #一定要加逗号,否则是分开的 if root.left: #self.ans += path dfs(root.left, path + "->" + str(root.left.val)) if root.right: dfs(root.right, path + "->" + str(root.right.val)) #此时path仍然是root的 dfs(root,str(root.val)) return self.ans
0 0
- leetcode 257: Binary Tree Paths
- leetCode #257 Binary Tree Paths
- LeetCode(257)Binary Tree Paths
- [leetcode 257]Binary Tree Paths
- leetcode[257] Binary Tree Paths
- LeetCode 257: Binary Tree Paths
- leetcode 257 : Binary Tree Paths
- 【LEETCODE】257-Binary Tree Paths
- [leetcode 257] Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- LeetCode 257:Binary Tree Paths
- leetcode 257:Binary Tree Paths
- leetcode-257-Binary Tree Paths
- LeetCode 257Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- leetcode 257 Binary Tree Paths
- LeetCode-257:Binary Tree Paths
- 备忘kettle
- long long unsigned long long 的最大值最小值在不同平台测试
- poj 3904(莫比乌斯反演)
- Android岗位的面试总结
- LeetCode Implement Trie (Prefix Tree) 字典树/前缀树
- 【LEETCODE】257-Binary Tree Paths
- 目录
- 个人细小知识点整理
- 机器学习----梯度下降法
- iOS 开发 自定义Tabbar
- struts之拦截器
- Jasmine-基于JavaScript的行为驱动单元测试框架简介
- Error:Execution failed for task ':app:compileDebugAidl'. > aidl is missing(Android Studio编译错误)
- 专为设计师而写的GitHub快速入门教程