LeetCode 257. Binary Tree Paths
原题链接在这里:https://leetcode.com/problems/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"]
题解:
DFS 依次添加,终止条件是叶子节点.
Note:1. If use StringBuilder, remember backtracking before each return.
e.g. [1,2,3], 会写成["1->2","1->23"]. 因为扫过点2,StringBuilder 会变成1->2, 扫过点3,原来的点2没有去掉,就会变成1->23,但是第一个结果没有变成1->23是因为,用到sb.toString()时自动做了copy by value.
2 . 但如果使用String,在递归调用时可以直接使用str, 因为String 和 int, float一样是priority type, 是copy by value,而不像array等object 是 copy by reference.
Time Complexity: O(n), 这是dfs.
Space: O(h). h是树的高度,一共用了h层stack. StringBuilder size.
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode() {} 8 * TreeNode(int val) { this.val = val; } 9 * TreeNode(int val, TreeNode left, TreeNode right) { 10 * this.val = val; 11 * this.left = left; 12 * this.right = right; 13 * } 14 * } 15 */ 16 class Solution { 17 public List<String> binaryTreePaths(TreeNode root) { 18 List<String> res = new ArrayList<>(); 19 if(root == null){ 20 return res; 21 } 22 23 dfs(root, new StringBuilder(), res); 24 return res; 25 } 26 27 private void dfs(TreeNode root, StringBuilder sb, List<String> res){ 28 if(root == null){ 29 return; 30 } 31 32 int len = sb.length(); 33 if(root.left == null && root.right == null){ 34 sb.append(root.val); 35 res.add(sb.toString()); 36 sb.setLength(len); 37 return; 38 } 39 40 sb.append(root.val).append("->"); 41 dfs(root.left, sb, res); 42 dfs(root.right, sb, res); 43 sb.setLength(len); 44 } 45 }
上面使用StringBuilder, 下面使用String.
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public List<String> binaryTreePaths(TreeNode root) { 12 List<String> res = new ArrayList<String>(); 13 if(root == null){ 14 return res; 15 } 16 dfs(root, "", res); 17 return res; 18 } 19 private void dfs(TreeNode root, String str, List<String> res){ 20 if(root.left == null && root.right == null){ 21 str = str + root.val; 22 res.add(str); 23 } 24 str = str + root.val + "->"; 25 if(root.left != null){ 26 dfs(root.left, str, res); 27 } 28 if(root.right != null){ 29 dfs(root.right, str, res); 30 } 31 } 32 }
AC C++:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 vector<string> binaryTreePaths(TreeNode* root) { 15 vector<string> res; 16 if(!root){ 17 return res; 18 } 19 20 dfs(root, "", res); 21 return res; 22 } 23 24 private: 25 void dfs(TreeNode* root, string item, vector<string>& res){ 26 if(!root){ 27 return; 28 } 29 30 if(!root->left && !root->right){ 31 item += to_string(root->val); 32 res.push_back(item); 33 return; 34 } 35 36 item += to_string(root->val) + "->"; 37 dfs(root->left, item, res); 38 dfs(root->right, item, res); 39 } 40 };
类似Path Sum, Smallest String Starting From Leaf, Sum Root to Leaf Numbers.