1. Full Level Order Traversal (Store All)
Intuition
Perform standard BFS collecting all node values into levels array levels[level][]. Then iterate through levels and take levels[i][0].
When to apply: Basic baseline check.
Algorithm
- Step 1: If root == null return [].
- Step 2: Run BFS with queue, collecting all node values level-by-level into a 2D array.
- Step 3: Extract the 0-th element of each level array.
- Step 4: Return the result list.
#include <iostream>#include <vector>#include <string>#include <algorithm>using namespace std; class Solution {public: vector<int> leftView(TreeNode* root) { if (!root) return {}; vector<vector<int>> levels; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int sz = q.size(); vector<int> cur; for (int i = 0; i < sz; ++i) { TreeNode* node = q.front(); q.pop(); cur.push_back(node->val); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } levels.push_back(cur); } vector<int> res; for (auto& lvl : levels) res.push_back(lvl[0]); return res; }}; int main() { // Sample driver execution cout << "Execution completed successfully." << endl; return 0;} Time & Space Complexity
- Time complexity:
O(n)— All n nodes are stored in the 2D levels array. - Space complexity:
O(n)— The 2D list stores all n nodes.