|
| 1 | +## 257. 二叉树的所有路径 |
| 2 | +> https://leetcode-cn.com/problems/binary-tree-paths/ |
| 3 | +
|
| 4 | + |
| 5 | +### Java |
| 6 | +```java |
| 7 | +/* |
| 8 | + * @Author: Goog Tech |
| 9 | + * @Date: 2020-08-16 17:00:50 |
| 10 | + * @LastEditTime: 2020-08-16 17:11:57 |
| 11 | + * @Description: https://leetcode-cn.com/problems/binary-tree-paths/ |
| 12 | + * @FilePath: \leetcode-googtech\#102. Binary Tree Level Order Traversal\257.二叉树的所有路径.java |
| 13 | + * @WebSite: https://algorithm.show/ |
| 14 | + */ |
| 15 | + |
| 16 | +/* |
| 17 | + * @lc app=leetcode.cn id=257 lang=java |
| 18 | + * |
| 19 | + * [257] 二叉树的所有路径 |
| 20 | + */ |
| 21 | + |
| 22 | +// @lc code=start |
| 23 | +/** |
| 24 | + * Definition for a binary tree node. |
| 25 | + * public class TreeNode { |
| 26 | + * int val; |
| 27 | + * TreeNode left; |
| 28 | + * TreeNode right; |
| 29 | + * TreeNode(int x) { val = x; } |
| 30 | + * } |
| 31 | + */ |
| 32 | +class Solution { |
| 33 | + |
| 34 | + public List<String> binaryTreePaths(TreeNode root) { |
| 35 | + LinkedList<String> paths = new LinkedList<>(); |
| 36 | + getPaths(root, "", paths); |
| 37 | + return paths; |
| 38 | + } |
| 39 | + |
| 40 | + private void getPaths(TreeNode root, String path, LinkedList<String> paths) { |
| 41 | + if(root !=null) { |
| 42 | + // 记录当前节点到叶子节点的路径 |
| 43 | + path += Integer.toString(root.val); |
| 44 | + // 判断当前节点是否为叶子节点,若是则将其整体添加到 List 集合中 |
| 45 | + if(root.left == null && root.right == null) { |
| 46 | + paths.add(path); |
| 47 | + // 反之则继续遍历当前节点的左右孩子节点 |
| 48 | + }else { |
| 49 | + path += "->"; |
| 50 | + getPaths(root.left, path, paths); |
| 51 | + getPaths(root.right, path, paths); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +// @lc code=end |
| 57 | +``` |
| 58 | + |
| 59 | +### Python |
| 60 | +```python |
| 61 | +''' |
| 62 | +Author: Goog Tech |
| 63 | +Date: 2020-08-16 16:58:35 |
| 64 | +LastEditTime: 2020-08-16 17:10:50 |
| 65 | +Description: https://leetcode-cn.com/problems/binary-tree-paths/ |
| 66 | +FilePath: \leetcode-googtech\#102. Binary Tree Level Order Traversal\257.二叉树的所有路径.py |
| 67 | +WebSite: https://algorithm.show/ |
| 68 | +''' |
| 69 | +# |
| 70 | +# @lc app=leetcode.cn id=257 lang=python |
| 71 | +# |
| 72 | +# [257] 二叉树的所有路径 |
| 73 | +# |
| 74 | + |
| 75 | +# @lc code=start |
| 76 | +# Definition for a binary tree node. |
| 77 | +# class TreeNode(object): |
| 78 | +# def __init__(self, x): |
| 79 | +# self.val = x |
| 80 | +# self.left = None |
| 81 | +# self.right = None |
| 82 | + |
| 83 | +class Solution(object): |
| 84 | + def binaryTreePaths(self, root): |
| 85 | + """ |
| 86 | + :type root: TreeNode |
| 87 | + :rtype: List[str] |
| 88 | + """ |
| 89 | + # 判断当前节点是否为空 |
| 90 | + if not root: |
| 91 | + return [] |
| 92 | + # 判断当前节点是否为叶子节点 |
| 93 | + if not root.left and not root.right: |
| 94 | + return [str(root.val)] |
| 95 | + # 初始化用于存储根节点到左右叶子节点路径的列表 |
| 96 | + paths = [] |
| 97 | + # 递归添加当前节点到叶子节点的路径 |
| 98 | + if root.left: |
| 99 | + for i in self.binaryTreePaths(root.left): |
| 100 | + paths.append(str(root.val) + '->' + i) |
| 101 | + if root.right: |
| 102 | + for i in self.binaryTreePaths(root.right): |
| 103 | + paths.append(str(root.val) + '->' + i) |
| 104 | + # 返回结果 |
| 105 | + return paths |
| 106 | +# @lc code=end |
| 107 | +``` |
0 commit comments