leetcode 257. Binary Tree Paths
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"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution( object ): def binaryTreePaths( self , root): """ :type root: TreeNode :rtype: List[str] """ def dfs(node, path, ans): if not node: return if not node.left and not node.right: # leaf node ans.append(path + str (node.val)) return dfs(node.left, path + str (node.val) + "->" , ans) dfs(node.right, path + str (node.val) + "->" , ans) ans = [] dfs(root, "", ans) return ans |
stack 迭代解法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution( object ): def binaryTreePaths( self , root): """ :type root: TreeNode :rtype: List[str] """ if not root: return [] stack = [root] paths = [""] ans = [] while stack: node = stack.pop() path = paths.pop() if not node.left and not node.right: ans.append(path + str (node.val)) if node.right: stack.append(node.right) paths.append(path + str (node.val) + "->" ) if node.left: stack.append(node.left) paths.append(path + str (node.val) + "->" ) return ans |
BFS:(和dfs stack解法就差if顺序)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution( object ): def binaryTreePaths( self , root): """ :type root: TreeNode :rtype: List[str] """ if not root: return [] q = collections.deque([root]) paths = collections.deque([""]) ans = [] while q: node = q.popleft() path = paths.popleft() if not node.left and not node.right: ans.append(path + str (node.val)) if node.left: q.append(node.left) paths.append(path + str (node.val) + "->" ) if node.right: q.append(node.right) paths.append(path + str (node.val) + "->" ) return ans |
其他解法还有不用helper函数的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution( object ): def binaryTreePaths( self , root): """ :type root: TreeNode :rtype: List[str] """ if not root: return [] if not root.left and not root.right: return [ str (root.val)] paths = [] for path in self .binaryTreePaths(root.left): paths.append( str (root.val) + '->' + path) for path in self .binaryTreePaths(root.right): paths.append( str (root.val) + '->' + path) return paths |
其他人代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | # dfs + stack def binaryTreePaths1( self , root): if not root: return [] res, stack = [], [(root, "")] while stack: node, ls = stack.pop() if not node.left and not node.right: res.append(ls + str (node.val)) if node.right: stack.append((node.right, ls + str (node.val) + "->" )) if node.left: stack.append((node.left, ls + str (node.val) + "->" )) return res # bfs + queue def binaryTreePaths2( self , root): if not root: return [] res, queue = [], collections.deque([(root, "")]) while queue: node, ls = queue.popleft() if not node.left and not node.right: res.append(ls + str (node.val)) if node.left: queue.append((node.left, ls + str (node.val) + "->" )) if node.right: queue.append((node.right, ls + str (node.val) + "->" )) return res # dfs recursively def binaryTreePaths( self , root): if not root: return [] res = [] self .dfs(root, "", res) return res def dfs( self , root, ls, res): if not root.left and not root.right: res.append(ls + str (root.val)) if root.left: self .dfs(root.left, ls + str (root.val) + "->" , res) if root.right: self .dfs(root.right, ls + str (root.val) + "->" , res) |
标签:
leetcode
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-04-14 echo 到 stderr