1. Introduction to Binary Tree Interview Questions
Binary trees are asked in almost every software engineering interview at companies like Amazon, Google, Microsoft, Meta, and TCS. They test your ability to think recursively, manage tree traversals (inorder, preorder, postorder, level-order), and solve tree modification problems.
Below are the Top 20 Binary Tree interview questions with complete solutions in C++, Java, Python, and JavaScript.
2. Tree Node Definition & Core Patterns
cppstruct TreeNode { int val; TreeNode *left, *right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} };
javaclass TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } }
pythonclass TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right
javascriptclass TreeNode { constructor(val = 0, left = null, right = null) { this.val = val; this.left = left; this.right = right; } }
3. Easy Binary Tree Questions
Q1. Maximum Depth of a Binary Tree
Question: Find the height (maximum depth) of a binary tree.
Intuition: Bottom-up DFS. The maximum depth of any node is 1 + max(depth(left), depth(right)). Base case: empty tree has depth 0.
cppint maxDepth(TreeNode* root) { if (!root) return 0; return 1 + max(maxDepth(root->left), maxDepth(root->right)); }
javapublic int maxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); }
pythondef maxDepth(root): if not root: return 0 return 1 + max(maxDepth(root.left), maxDepth(root.right))
javascriptfunction maxDepth(root) { if (!root) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); }
Time Complexity: O(n) | Space Complexity: O(h)
Q2. Invert a Binary Tree
Question: Swap the left and right children of every node in the binary tree.
cppTreeNode* invertTree(TreeNode* root) { if (!root) return nullptr; TreeNode* temp = root->left; root->left = invertTree(root->right); root->right = invertTree(temp); return root; }
javapublic TreeNode invertTree(TreeNode root) { if (root == null) return null; TreeNode temp = root.left; root.left = invertTree(root.right); root.right = invertTree(temp); return root; }
pythondef invertTree(root): if not root: return None root.left, root.right = invertTree(root.right), invertTree(root.left) return root
javascriptfunction invertTree(root) { if (!root) return null; let temp = root.left; root.left = invertTree(root.right); root.right = invertTree(temp); return root; }
Time Complexity: O(n) | Space Complexity: O(h)
Q3. Same Tree
Question: Given the roots of two binary trees p and q, check if they are identical in structure and values.
cppbool isSameTree(TreeNode* p, TreeNode* q) { if (!p && !q) return true; if (!p || !q || p->val != q->val) return false; return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); }
javapublic boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null || p.val != q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }
pythondef isSameTree(p, q): if not p and not q: return True if not p or not q or p.val != q.val: return False return isSameTree(p.left, q.left) and isSameTree(p.right, q.right)
javascriptfunction isSameTree(p, q) { if (!p && !q) return true; if (!p || !q || p.val !== q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }
Time Complexity: O(n) | Space Complexity: O(h)
Q4. Symmetric Tree
Question: Check whether a binary tree is a mirror of itself (symmetric around its center).
cppbool isMirror(TreeNode* t1, TreeNode* t2) { if (!t1 && !t2) return true; if (!t1 || !t2 || t1->val != t2->val) return false; return isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left); } bool isSymmetric(TreeNode* root) { return isMirror(root, root); }
javapublic boolean isMirror(TreeNode t1, TreeNode t2) { if (t1 == null && t2 == null) return true; if (t1 == null || t2 == null || t1.val != t2.val) return false; return isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left); } public boolean isSymmetric(TreeNode root) { return isMirror(root, root); }
pythondef isSymmetric(root): def isMirror(t1, t2): if not t1 and not t2: return True if not t1 or not t2 or t1.val != t2.val: return False return isMirror(t1.left, t2.right) and isMirror(t1.right, t2.left) return isMirror(root, root)
javascriptfunction isSymmetric(root) { function isMirror(t1, t2) { if (!t1 && !t2) return true; if (!t1 || !t2 || t1.val !== t2.val) return false; return isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left); } return isMirror(root, root); }
Time Complexity: O(n) | Space Complexity: O(h)
4. Medium Binary Tree Questions
Q5. Binary Tree Level Order Traversal (BFS)
Question: Return the level-order traversal of its nodes' values (i.e., from left to right, level by level).
cpp#include <vector> #include <queue> using namespace std; vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if (!root) return res; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int sz = q.size(); vector<int> level; for (int i = 0; i < sz; i++) { TreeNode* curr = q.front(); q.pop(); level.push_back(curr->val); if (curr->left) q.push(curr->left); if (curr->right) q.push(curr->right); } res.push_back(level); } return res; }
javaimport java.util.*; public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) return res; Queue<TreeNode> q = new LinkedList<>(); q.add(root); while (!q.isEmpty()) { int sz = q.size(); List<Integer> level = new ArrayList<>(); for (int i = 0; i < sz; i++) { TreeNode curr = q.poll(); level.add(curr.val); if (curr.left != null) q.add(curr.left); if (curr.right != null) q.add(curr.right); } res.add(level); } return res; }
pythonfrom collections import deque def levelOrder(root): if not root: return [] res = [] q = deque([root]) while q: level = [] for _ in range(len(q)): curr = q.popleft() level.append(curr.val) if curr.left: q.append(curr.left) if curr.right: q.append(curr.right) res.append(level) return res
javascriptfunction levelOrder(root) { if (!root) return []; let res = []; let q = [root]; while (q.length > 0) { let sz = q.length; let level = []; for (let i = 0; i < sz; i++) { let curr = q.shift(); level.push(curr.val); if (curr.left) q.push(curr.left); if (curr.right) q.push(curr.right); } res.push(level); } return res; }
Time Complexity: O(n) | Space Complexity: O(n)
Q6. Lowest Common Ancestor (LCA) of a Binary Tree
Question: Find the lowest common ancestor of two given nodes p and q.
cppTreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root || root == p || root == q) return root; TreeNode* left = lowestCommonAncestor(root->left, p, q); TreeNode* right = lowestCommonAncestor(root->right, p, q); if (left && right) return root; return left ? left : right; }
javapublic TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) return root; TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if (left != null && right != null) return root; return (left != null) ? left : right; }
pythondef lowestCommonAncestor(root, p, q): if not root or root == p or root == q: return root left = lowestCommonAncestor(root.left, p, q) right = lowestCommonAncestor(root.right, p, q) if left and right: return root return left or right
javascriptfunction lowestCommonAncestor(root, p, q) { if (!root || root === p || root === q) return root; let left = lowestCommonAncestor(root.left, p, q); let right = lowestCommonAncestor(root.right, p, q); if (left && right) return root; return left ? left : right; }
Time Complexity: O(n) | Space Complexity: O(h)
Q7. Diameter of a Binary Tree
Question: Find the length of the longest path between any two nodes in a tree.
cppint diameter = 0; int maxPath(TreeNode* root) { if (!root) return 0; int left = maxPath(root->left); int right = maxPath(root->right); diameter = max(diameter, left + right); return 1 + max(left, right); } int diameterOfBinaryTree(TreeNode* root) { diameter = 0; maxPath(root); return diameter; }
javaclass Solution { int diameter = 0; private int maxPath(TreeNode root) { if (root == null) return 0; int left = maxPath(root.left); int right = maxPath(root.right); diameter = Math.max(diameter, left + right); return 1 + Math.max(left, right); } public int diameterOfBinaryTree(TreeNode root) { diameter = 0; maxPath(root); return diameter; } }
pythondef diameterOfBinaryTree(root): diameter = 0 def maxPath(node): nonlocal diameter if not node: return 0 left = maxPath(node.left) right = maxPath(node.right) diameter = max(diameter, left + right) return 1 + max(left, right) maxPath(root) return diameter
javascriptfunction diameterOfBinaryTree(root) { let diameter = 0; function maxPath(node) { if (!node) return 0; let left = maxPath(node.left); let right = maxPath(node.right); diameter = Math.max(diameter, left + right); return 1 + Math.max(left, right); } maxPath(root); return diameter; }
Time Complexity: O(n) | Space Complexity: O(h)
Q8. Validate Binary Search Tree (BST)
Question: Determine if a given binary tree is a valid Binary Search Tree.
cppbool validate(TreeNode* node, long long minVal, long long maxVal) { if (!node) return true; if (node->val <= minVal || node->val >= maxVal) return false; return validate(node->left, minVal, node->val) && validate(node->right, node->val, maxVal); } bool isValidBST(TreeNode* root) { return validate(root, LONG_MIN, LONG_MAX); }
javapublic boolean validate(TreeNode node, Long minVal, Long maxVal) { if (node == null) return true; if ((minVal != null && node.val <= minVal) || (maxVal != null && node.val >= maxVal)) return false; return validate(node.left, minVal, (long)node.val) && validate(node.right, (long)node.val, maxVal); } public boolean isValidBST(TreeNode root) { return validate(root, null, null); }
pythondef isValidBST(root): def validate(node, min_val, max_val): if not node: return True if node.val <= min_val or node.val >= max_val: return False return validate(node.left, min_val, node.val) and validate(node.right, node.val, max_val) return validate(root, float('-inf'), float('inf'))
javascriptfunction isValidBST(root) { function validate(node, minVal, maxVal) { if (!node) return true; if (node.val <= minVal || node.val >= maxVal) return false; return validate(node.left, minVal, node.val) && validate(node.right, node.val, maxVal); } return validate(root, -Infinity, Infinity); }
Time Complexity: O(n) | Space Complexity: O(h)
5. Summary Table
| Problem | Approach | Time | Space |
|---|---|---|---|
| Max Depth | Bottom-up DFS | O(n) | O(h) |
| Invert Tree | Recursive Swap | O(n) | O(h) |
| Level Order Traversal | Queue (BFS) | O(n) | O(n) |
| Lowest Common Ancestor | DFS recursion | O(n) | O(h) |
| Validate BST | Range-bounded DFS | O(n) | O(h) |
Practice all tree problems on DSAMaster's practice platform.
