给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
输入:root = [1,2,3,null,5]
输出:["1->2->5","1->3"]
输入:root = [1]
输出:["1"]
提示:
树中节点的数目在范围 [1, 100] 内
-100 <= Node.val <= 100
最直观的方法是使用深度优先搜索。在深度优先搜索遍历二叉树时,我们需要考虑当前的节点以及它的孩子节点。
如果当前节点不是叶子节点,则在当前的路径末尾添加该节点,并继续递归遍历该节点的每一个孩子节点。
如果当前节点是叶子节点,则在当前路径末尾添加该节点后我们就得到了一条从根节点到叶子节点的路径,将该路径加入到答案即可。
递归二步曲:
(1) 找出重复的子问题。
(2) 确定终止条件。
对于二叉树的所有路径中的每条路径,当遍历到叶子节点的时候为当前路径的结束。并且将当前路径加入结果集。
class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> paths = new ArrayList<String>(); constructPaths(root, "", paths); return paths; } public void constructPaths(TreeNode root, String path, List<String> paths) { if (root != null) { StringBuffer pathSB = new StringBuffer(path); pathSB.append(Integer.toString(root.val)); if (root.left == null && root.right == null) { // 当前节点是叶子节点 paths.add(pathSB.toString()); // 把路径加入到答案中 } else { pathSB.append("->"); // 当前节点不是叶子节点,继续递归遍历 constructPaths(root.left, pathSB.toString(), paths); constructPaths(root.right, pathSB.toString(), paths); } } } }
时间复杂度:O(N^2)
空间复杂度:O(N^2)
我们也可以用广度优先搜索来实现。
func binaryTreePaths(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 < len(nodeQueue); i++ { node, path := nodeQueue[i], pathQueue[i] if node.Left == nil && node.Right == nil { paths = append(paths, path) continue } if node.Left != nil { nodeQueue = append(nodeQueue, node.Left) pathQueue = append(pathQueue, path + "->" + 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 }
时间复杂度:O(N^2)
空间复杂度:O(N^2)