0257-二叉树的所有路径
给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [1,2,3,null,5]
输出:["1->2->5","1->3"]
示例 2:
输入:root = [1]
输出:["1"]
提示:
树中节点的数目在范围 [1, 100] 内
-100 <= Node.val <= 100
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-paths
参考:
- https://leetcode-cn.com/problems/binary-tree-paths/solution/er-cha-shu-de-suo-you-lu-jing-by-leetcode-solution/
- https://leetcode-cn.com/problems/binary-tree-paths/solution/dai-ma-sui-xiang-lu-dai-ni-xue-tou-er-ch-cxmu/
python
# 0257.二叉树的所有路径
class Solution:
def binaryTreePaths(self, root: TreeNode) -> [str]:
path = []
res = []
def backtrace(root, path):
if not root:
return
path.append(root.val)
if not root.left and not root.right:
res.append(path[:])
ways = []
if root.left:
ways.append(root.left)
if root.right:
ways.append(root.right)
for way in ways:
backtrace(way, path)
path.pop()
backtrace(root, path)
return [ "->".join(list(map(str, i))) for i in res ]
def binaryTreePaths_DFS(self, root: TreeNode) -> [str]:
def construct_paths(root, path):
if root:
path += str(root.val)
if not root.left and not root.right: # 叶子结点
paths.append(path) # 路径加入结果集
else: # 非叶子节点,继续递归
path += "->"
construct_paths(root.left, path)
construct_paths(root.right, path)
paths = []
construct_paths(root, "")
return paths
def binaryTreePaths_BFS(self, root: TreeNode) -> [str]:
paths = []
if not root:
return paths
from collections import deque
node_queue = deque([root])
path_queue = deque([str(root.val)])
while node_queue:
node = node_queue.popleft()
path = path_queue.popleft()
if not node.left and not node.right:
paths.append(path)
else:
if node.left:
node_queue.append(node.left)
path_queue.append(path + "->" + str(node.left.val))
if node.right:
node_queue.append(node.right)
path_queue.append(path + "->" + str(node.right.val))
return paths
golang
package binaryTree
import "strconv"
func binaryTreePaths(root *TreeNode) []string {
res := make([]string, 0)
var travel func(node *TreeNode, s string)
travel= func(node *TreeNode, s string) {
if node.Left == nil && node.Right == nil {
v := s + strconv.Itoa(node.Val)
res = append(res, v)
return
}
s = s + strconv.Itoa(node.Val) + "->"
if node.Left != nil {
travel(node.Left, s)
}
if node.Right != nil {
travel(node.Right, s)
}
}
travel(root, "")
return res
}
func binaryTreePathsDFS(root *TreeNode) []string {
paths := []string{}
construcPaths(root, "")
return paths
}
func construcPaths(root *TreeNode, path string) {
if root != nil {
pathSB := path
pathSB += strconv.Itoa(root.Val)
if root.Left == nil && root.Right == nil {
paths = append(paths, pathSB)
} else {
pathSB += "->"
construcPaths(root.Left, pathSB)
construcPaths(root.Right, pathSB)
}
}
}
func binaryTreePathsBFS(root *TreeNode) []string {
paths := []string{}
if root == nil {
return paths
}
nodeQueue := []*TreeNode{}
pathQueue := []string{}
nodeQueue = append(nodeQueue, root)
pathQueue = append(pathQueue, strconv.Itoa(root.Val))
for i:=0;i" +strconv.Itoa(node.Left.Val))
}
if node.Right != nil {
nodeQueue =append(nodeQueue, node.Right)
pathQueue = append(pathQueue, path + "->" +strconv.Itoa(node.Right.Val))
}
}
return paths
}