A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 144. Binary Tree Preorder Traversal, 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, stack, sliding window.
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 preorder, preorderTraversal.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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//Runtime: 4 ms, faster than 50.88% of C++ online submissions for Binary Tree Preorder Traversal.
02//Memory Usage: 9 MB, less than 9.54% of C++ online submissions for Binary Tree Preorder Traversal.
03/**
04 * Definition for a binary tree node.
05 * struct TreeNode {
06 * int val;
07 * TreeNode *left;
08 * TreeNode *right;
09 * TreeNode() : val(0), left(nullptr), right(nullptr) {}
10 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
12 * };
13 */
14class Solution {
15public:
16 vector<int> ans;
17
18 void preorder(TreeNode* node){
19 if(!node) return;
20 ans.push_back(node->val);
21 preorder(node->left);
22 preorder(node->right);
23 }
24
25 vector<int> preorderTraversal(TreeNode* root) {
26 preorder(root);
27 return ans;
28 }
29};
30
31//iterative
32//Runtime: 4 ms, faster than 50.88% of C++ online submissions for Binary Tree Preorder Traversal.
33//Memory Usage: 8.8 MB, less than 9.54% of C++ online submissions for Binary Tree Preorder Traversal.
34class Solution {
35public:
36 vector<int> preorderTraversal(TreeNode* root) {
37 vector<int> ans;
38 if(!root) return ans;
39
40 stack<TreeNode*> stk;
41
42 stk.push(root);
43
44 TreeNode* cur;
45
46 while(!stk.empty()){
47 cur = stk.top(); stk.pop();
48
49 ans.emplace_back(cur->val);
50 if(cur->right) stk.push(cur->right);
51 if(cur->left) stk.push(cur->left);
52 }
53
54 return ans;
55 }
56};
Cost