Given a binary tree root, return the leftmost node’s value on each level of the tree.
Constraints
n ≤ 100,000 where n is the number of nodes in root
Example 1
Input
Visualize Tree
root =0 5 2 1Output
[0, 5, 1]
Tree problems can often be solved using Depth First Search (DFS) algorithm or Breadth First Search (BFS) Algorithm. These are two typical tree traversal algorithms which can also be applied to graph problems.
DFS to Compute the Left Side View of a Binary Tree
The key to solve this problem is that the first time of a new level of node is met, we can push the node to the left-side view. The tree travesal should be pre-order, where we visit the root node and then its left child and right child respectively.
C++ Algorithm to implement DFS to compute the left-side view of the binary tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * class Tree { * public: * int val; * Tree *left; * Tree *right; * }; */ vector<int> solve(Tree* root) { if (!root) return {}; vector<int> ans; function<void(Tree*, int)> dfs = [&](Tree* root, int level) { if (!root) return; // first time which is the left-most node if (level >= ans.size()) { ans.push_back(root->val); } dfs(root->left, level + 1); dfs(root->right, level + 1); }; dfs(root, 0); return ans; } |
/** * class Tree { * public: * int val; * Tree *left; * Tree *right; * }; */ vector<int> solve(Tree* root) { if (!root) return {}; vector<int> ans; function<void(Tree*, int)> dfs = [&](Tree* root, int level) { if (!root) return; // first time which is the left-most node if (level >= ans.size()) { ans.push_back(root->val); } dfs(root->left, level + 1); dfs(root->right, level + 1); }; dfs(root, 0); return ans; }
Compute the left-most view of a binary tree using Python programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # class Tree: # def __init__(self, val, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def solve(self, root): ans = [] def dfs(root, level): if root is None: return # first time left-most node if len(ans) <= level: ans.append(root.val) dfs(root.left, level + 1) dfs(root.right, level + 1) dfs(root, 0) return ans |
# class Tree: # def __init__(self, val, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def solve(self, root): ans = [] def dfs(root, level): if root is None: return # first time left-most node if len(ans) <= level: ans.append(root.val) dfs(root.left, level + 1) dfs(root.right, level + 1) dfs(root, 0) return ans
Left-side View of a Binary Tree using Breadth First Search Algorithm
If we are using BFS (Breadth First Search Algorithm), we can just push the first node of each level to the left-side view. This requires us to only expand the children of the current nodes in the queue – which are of the same level.
C++ code to implement a BFS to compute the left-side view of a binary tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * class Tree { * public: * int val; * Tree *left; * Tree *right; * }; */ vector<int> solve(Tree* root) { if (!root) return {}; queue<Tree*> Q; Q.push(root); vector<int> res{}; while (!Q.empty()) { auto sz = Q.size(); // first node in the queue is the left-most node auto p = Q.front(); res.push_back(p->val); for (auto i = 0; i < sz; ++ i) { auto p = Q.front(); if (p->left) { Q.push(p->left); } if (p->right) { Q.push(p->right); } Q.pop(); } } return res; } |
/** * class Tree { * public: * int val; * Tree *left; * Tree *right; * }; */ vector<int> solve(Tree* root) { if (!root) return {}; queue<Tree*> Q; Q.push(root); vector<int> res{}; while (!Q.empty()) { auto sz = Q.size(); // first node in the queue is the left-most node auto p = Q.front(); res.push_back(p->val); for (auto i = 0; i < sz; ++ i) { auto p = Q.front(); if (p->left) { Q.push(p->left); } if (p->right) { Q.push(p->right); } Q.pop(); } } return res; }
And below is the Python code to achieve same thing using the same BFS Algorithm:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # class Tree: # def __init__(self, val, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def solve(self, root): if root is None: return [] ans = [] q = [root] while len(q) > 0: sz = len(q) ans.append(q[0].val) for i in range(sz): p = q.pop(0) if p.left: q.append(p.left) if p.right: q.append(p.right) return ans |
# class Tree: # def __init__(self, val, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def solve(self, root): if root is None: return [] ans = [] q = [root] while len(q) > 0: sz = len(q) ans.append(q[0].val) for i in range(sz): p = q.pop(0) if p.left: q.append(p.left) if p.right: q.append(p.right) return ans
Both DFS and BFS run in time O(N) and space O(N) where N is the number of the nodes in the binary tree.
Left or Right Side View of a Binary Tree
- Teaching Kids Programming – The Left Side View of Binary Tree via Breadth First Search Algorithm
- Depth First Search and Breadth First Search Algorithm to Compute the Left Side View of a Binary Tree
- Teaching Kids Programming – Left/Right Side View of a Binary Tree using Depth/Breadth First Search Algorithms
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Teaching Kids Programming - How to Check if Two Strings Anagrams?
Next Post: Teaching Kids Programming - Algorithms of Greatest Common Divisor and Least Common Multiples