DSAMaster Logo
DSAMaster
Trees3 August 202620 min read

Top 20 Binary Tree Interview Questions and Answers (2026)

Crack binary tree interview questions with confidence. This guide covers the top 20 tree interview questions with clean code in C++, Java, Python, and JavaScript, DFS/BFS patterns, BST operations, and complexity analysis.

D
Written by DSAMaster Team
DSAMaster Editorial

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

javascript
class 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.

javascript
function 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.

javascript
function 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.

javascript
function 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).

javascript
function 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).

javascript
function 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.

javascript
function 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.

javascript
function 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.

javascript
function 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

ProblemApproachTimeSpace
Max DepthBottom-up DFSO(n)O(h)
Invert TreeRecursive SwapO(n)O(h)
Level Order TraversalQueue (BFS)O(n)O(n)
Lowest Common AncestorDFS recursionO(n)O(h)
Validate BSTRange-bounded DFSO(n)O(h)

Practice all tree problems on DSAMaster's practice platform.