I like to read this solution as a small machine: keep the useful information, throw away the noise. For 257. Binary Tree Paths, the solution in this repository is mainly a two pointers solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are binaryTreePaths.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Given a binary tree, return all root-to-leaf paths.
03
04Note: A leaf is a node with no children.
05
06Example:
07
08Input:
09
10 1
11 / \
122 3
13 \
14 5
15
16Output: ["1->2->5", "1->3"]
17
18Explanation: All root-to-leaf paths are: 1->2->5, 1->3
19**/
20
21//Runtime: 8 ms, faster than 98.26% of C++ online submissions for Binary Tree Paths.
22//Memory Usage: 11.7 MB, less than 91.86% of C++ online submissions for Binary Tree Paths.
23
24/**
25 * Definition for a binary tree node.
26 * struct TreeNode {
27 * int val;
28 * TreeNode *left;
29 * TreeNode *right;
30 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
31 * };
32 */
33class Solution {
34public:
35 vector<string> binaryTreePaths(TreeNode* root) {
36 if(root == NULL) return vector<string>();
37 //leaf node
38 if(!root->left && !root->right)
39 return vector<string> {to_string(root->val)};
40 vector<string> ans;
41 if(root->left){
42 for(string s : binaryTreePaths(root->left)){
43 ans.push_back(to_string(root->val)+"->"+s);
44 // cout << to_string(root->val)+"->"+s << endl;
45 }
46 }
47 if(root->right){
48 for(string s : binaryTreePaths(root->right)){
49 ans.push_back(to_string(root->val)+"->"+s);
50 // cout << to_string(root->val)+"->"+s << endl;
51 }
52 }
53 return ans;
54 }
55};
Cost