Skip to main content

Simply Traverse

Simply DFS​

2236. Root Equals Sum of Children​

Easy·

The tree is guaranteed to hold exactly three nodes - a root with both children present - so there is nothing to traverse. Read the three values and compare.

FIG. ROOT EQUALS SUM OF CHILDREN● INTERACTIVE
visualization loads as you reach it
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def checkTree(self, root: TreeNode | None) -> bool:
return root.val == root.left.val + root.right.val

Sum of Binary Tree​

Basic·

Calculate the sum of all node values in a binary tree by traversing each node and adding its value to the total sum.

3 Approaches
FIG. SUM OF BINARY TREE● INTERACTIVE
visualization loads as you reach it
def sumBT(self, root):
def dfs(node):
if not node:
return 0
return node.data + dfs(node.left) + dfs(node.right)
 
return dfs(root)

Size of Binary Tree​

Basic·

Count the total number of nodes in a binary tree. Each node contributes 1 to the count, regardless of its value.

3 Approaches
FIG. SIZE OF BINARY TREE● INTERACTIVE
visualization loads as you reach it
def getSize(self, root: Optional["Node"]) -> int:
def dfs(node):
if not node:
return 0
return 1 + dfs(node.left) + dfs(node.right)
 
return dfs(root)

Count Leaves in Binary Tree​

Basic·

Count leaf nodes (nodes with no children) in a binary tree. A leaf node has both left and right children as null.

3 Approaches
FIG. COUNT LEAVES IN BINARY TREE● INTERACTIVE
visualization loads as you reach it
def countLeaves(self, root):
def dfs(node):
if not node:
return 0
if not node.left and not node.right:
return 1
return dfs(node.left) + dfs(node.right)
 
return dfs(root)

Count Non-Leaf Nodes in Tree​

Basic·

Count internal nodes (non-leaf nodes) in a binary tree. A non-leaf node has at least one child (left or right).

3 Approaches
FIG. COUNT NON LEAF NODES IN TREE● INTERACTIVE
visualization loads as you reach it
def countNonLeafNodes(self, root):
def dfs(node):
if not node:
return 0
if not node.left and not node.right:
return 0
return 1 + dfs(node.left) + dfs(node.right)
 
return dfs(root)

Sum of Leaf Nodes​

Easy·

Calculate the sum of all leaf node values in a binary tree. A leaf node has no children (both left and right are null).

3 Approaches
FIG. SUM OF LEAF NODES● INTERACTIVE
visualization loads as you reach it
def leafSum(self, root):
def dfs(node):
if not node:
return 0
if not node.left and not node.right:
return node.data
return dfs(node.left) + dfs(node.right)
 
return dfs(root)

Max and min element in Binary Tree​

Easy·

Find the maximum and minimum values among all nodes in a binary tree by comparing each node's value during traversal.

3 Approaches
FIG. MAX AND MIN ELEMENT IN BINARY TREE RECURSIVE● INTERACTIVE
visualization loads as you reach it
def findMax(self, root):
def dfs(node):
if not node:
return -float("inf")
return max(node.data, dfs(node.left), dfs(node.right))
 
return dfs(root)
 
 
def findMin(self, root):
def dfs(node):
if not node:
return float("inf")
return min(node.data, dfs(node.left), dfs(node.right))
 
return dfs(root)

Vertical Width of a Binary Tree​

Medium·

Find the vertical width of a binary tree. The vertical width is the number of vertical columns needed to display the tree, where each node is assigned a column based on its horizontal distance from the root.

3 Approaches
FIG. VERTICAL WIDTH OF BINARY TREE● INTERACTIVE
visualization loads as you reach it
def verticalWidth(self, root):
def dfs(node, col):
nonlocal mini, maxi
if not node:
return
mini, maxi = min(mini, col), max(maxi, col)
dfs(node.left, col - 1)
dfs(node.right, col + 1)
 
mini, maxi = float("inf"), -float("inf")
dfs(root, 0)
return maxi - mini + 1 if mini != float("inf") else 0

1469. Find All The Lonely Nodes​

Easy·

A lonely node is a node that is the only child of its parent node. Find all lonely nodes in a binary tree. The root node is never lonely as it has no parent.

3 Approaches
FIG. FIND ALL THE LONELY NODES● INTERACTIVE
visualization loads as you reach it
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
def dfs(node):
if not node:
return
if not node.left and node.right:
lonely.append(node.right.val)
if node.left and not node.right:
lonely.append(node.left.val)
dfs(node.left)
dfs(node.right)
 
lonely = []
dfs(root)
return lonely

965. Univalued Binary Tree​

Easy·

A binary tree is univalued if every node in the tree has the same value. Check if a given binary tree is univalued.

3 Approaches
FIG. UNIVALUED BINARY TREE RECURSIVE● INTERACTIVE
visualization loads as you reach it
def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
def dfs(node):
if not node:
return True
left_result = dfs(node.left)
right_result = dfs(node.right)
return left_result and right_result and root.val == node.val
 
return dfs(root)

404. Sum of Left Leaves​

Easy·

Given the root of a binary tree, return the sum of all left leaves. A left leaf is a leaf which is the left child of another node.

3 Approaches
FIG. SUM OF LEFT LEAVES● INTERACTIVE
visualization loads as you reach it
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
def dfs(node):
if not node:
return 0
total = node.left.val if is_leaf(node.left) else 0
total += dfs(node.left)
total += dfs(node.right)
return total
 
is_leaf = lambda node: node and not node.left and not node.right
 
return dfs(root)

1315. Sum of Nodes with Even-Valued Grandparent​

Medium·

Given the root of a binary tree, return the sum of values of nodes with even-valued grandparent. A grandparent is the parent of a parent of a node.

3 Approaches
FIG. SUM OF NODES WITH EVEN VALUED GRANDPAREN● INTERACTIVE
visualization loads as you reach it
def sumEvenGrandparent(self, root: Optional[TreeNode]) -> int:
def dfs(node, parent, gparent):
if not node:
return 0
left_result = dfs(node.left, node.val, parent)
right_result = dfs(node.right, node.val, parent)
current = node.val if gparent % 2 == 0 else 0
return current + left_result + right_result
 
return dfs(root, -1, -1)

671. Second Minimum Node In a Binary Tree​

Easy·
3 Approaches
FIG. SECOND MINIMUM NODE IN A BINARY TREE RECURSIVE● INTERACTIVE
visualization loads as you reach it
def findSecondMinimumValue(self, root: Optional[TreeNode]) -> int:
def rec(node):
nonlocal mini
if not node:
return -1
if node.val != root.val:
mini = min(mini, node.val)
rec(node.left) if node.left else None
rec(node.right) if node.right else None
 
mini = float("inf")
rec(root)
return mini if mini != float("inf") else -1

Simply BFS​

637. Average of Levels in Binary Tree​

Easy·

Given the root of a binary tree, return the average value of the nodes on each level in the form of an array.

3 Approaches
FIG. 637 AVERAGE OF LEVELS● INTERACTIVE
visualization loads as you reach it
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
queue = collections.deque([root])
levels = []
while queue:
total = count = 0
for _ in range(len(queue)):
node = queue.popleft()
if node:
total += node.val
count += 1
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
if count:
levels.append(total / count)
return levels

1302. Deepest Leaves Sum​

Medium·

Given the root of a binary tree, return the sum of values of its deepest leaves.

3 Approaches
FIG. DEEPEST LEAVES SUM● INTERACTIVE
visualization loads as you reach it
def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
queue = collections.deque([root])
total = 0
while queue:
total = 0
for _ in range(len(queue)):
node = queue.popleft()
if node:
if not node.left and not node.right:
total += node.val
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
return total

1161. Maximum Level Sum of a Binary Tree​

Medium·

Given the root of a binary tree, return the number of the level that has the maximum sum (1-indexed).

3 Approaches
FIG. MAXIMUM LEVEL SUM OF BINARY TREE● INTERACTIVE
visualization loads as you reach it
def maxLevelSum(self, root: Optional[TreeNode]) -> int:
queue = collections.deque([root])
level = maxi_level = 1
maxi = -float("inf")
while queue:
total = 0
for _ in range(len(queue)):
node = queue.popleft()
if node:
total += node.val
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
if maxi < total:
maxi = total
maxi_level = level
level += 1
return maxi_level

3157. Find the Level of Tree with Minimum Sum​

Medium·

Given the root of a binary tree, return the number of the level that has the minimum sum (1-indexed).

3 Approaches
FIG. FIND THE LEVEL OF TREE WITH MINIMUM SUM● INTERACTIVE
visualization loads as you reach it
def minimumLevel(self, root: Optional[TreeNode]) -> int:
queue = collections.deque([root])
level, ans = 1, 0
min_sum = float("inf")
while queue:
level_total = 0
for _ in range(len(queue)):
node = queue.popleft()
if node:
level_total += node.val
queue.append(node.left)
queue.append(node.right)
if queue and level_total < min_sum:
min_sum = level_total
ans = level
level += 1
return ans

Max Level Sum in Binary Tree​

Easy·

Find the level in a binary tree that has the maximum sum of node values. Return the maximum sum found across all levels.

3 Approaches
FIG. MAX LEVEL SUM IN BINARY TREE● INTERACTIVE
visualization loads as you reach it
def maxLevelSum(self, root):
queue = collections.deque([root])
maxi = -float("inf")
while queue:
total = None
for _ in range(len(queue)):
node = queue.popleft()
if node:
total = node.data + (total if total else 0)
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
if total:
maxi = max(maxi, total)
return maxi

Largest value in each level​

Easy·

Find the largest value in each level of a binary tree and return them as a list.

3 Approaches
FIG. LARGEST VALUE IN EACH LEVEL● INTERACTIVE
visualization loads as you reach it
def largestValues(self, root):
queue = collections.deque([root])
result = []
while queue:
maxi = -float("inf")
for _ in range(len(queue)):
node = queue.popleft()
if node:
maxi = max(maxi, node.data)
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
if maxi != -float("inf"):
result.append(maxi)
return result

Sum of Leaf Nodes at Min Level​

Easy·

Find the minimum level where leaf nodes exist, then sum all leaf node values at that specific level. A leaf node has no left or right children.

3 Approaches
FIG. SUM OF LEAF NODES AT MIN LEVEL● INTERACTIVE
visualization loads as you reach it
def minLeafSum(self, root):
queue = collections.deque([root])
total = 0
while queue:
for _ in range(len(queue)):
node = queue.popleft()
if node:
if not node.left and not node.right:
total += node.data
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
if total != 0:
return total
return 0

Odd even level difference​

Easy·

Calculate the sum of all nodes at odd-numbered levels (1, 3, 5, ...) and subtract the sum of all nodes at even-numbered levels (2, 4, 6, ...). Root is at level 1 (odd level).

3 Approaches
FIG. ODD EVEN LEVEL DIFFERENCE● INTERACTIVE
visualization loads as you reach it
def getLevelDiff(self, root):
queue = collections.deque([root])
odd = even = 0
is_odd = True
while queue:
for _ in range(len(queue)):
node = queue.popleft()
if node:
if is_odd:
odd += node.data
else:
even += node.data
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
is_odd = not is_odd
return odd - even

Maximum Width of Tree​

Easy·

Find the maximum number of nodes present at any level in a binary tree. The width of a level is the total number of nodes (including null nodes) at that level.

3 Approaches
FIG. MAXIMUM WIDTH OF TREE● INTERACTIVE
visualization loads as you reach it
def maxWidth(self, root):
queue = collections.deque([root])
max_width = 0
while queue:
width = 0
for _ in range(len(queue)):
node = queue.popleft()
if node:
width += 1
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
max_width = max(max_width, width)
return max_width

Maximum Node Level​

Easy·

Find the level in a binary tree that has the maximum number of nodes. Return the level number (0-indexed from root).

3 Approaches
FIG. MAXIMUM NODE LEVEL● INTERACTIVE
visualization loads as you reach it
def maxNodeLevel(self, root):
queue = collections.deque([root])
level = max_level = max_level_length = 0
while queue:
length = 0
for _ in range(len(queue)):
node = queue.popleft()
if node:
length += 1
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
if max_level_length < length:
max_level_length = length
max_level = level
level += 1
return max_level

Level of a Node in Binary Tree​

Easy·

Find the level of a given node in a binary tree. Return the level (1-indexed from root) if the node exists, otherwise return 0.

3 Approaches
FIG. LEVEL OF A NODE IN BINARY TREE● INTERACTIVE
visualization loads as you reach it
def getLevel(self, root, target):
queue = collections.deque([root])
level = 1
while queue:
for _ in range(len(queue)):
node = queue.popleft()
if node:
if node.data == target:
return level
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
level += 1
return 0

Leaves at Same Level or Not​

Easy·

Check if all leaf nodes in a binary tree are at the same level. Return True if all leaves are at the same level, False otherwise.

3 Approaches
FIG. LEAF AT SAME LEVEL● INTERACTIVE
visualization loads as you reach it
def check(self, root):
queue = collections.deque([root])
first_leaf_level = None
level = 1
while queue:
for _ in range(len(queue)):
node = queue.popleft()
if node:
if not node.left and not node.right:
if not first_leaf_level:
first_leaf_level = level
elif first_leaf_level != level:
return False
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
level += 1
return True

Nodes at Odd Levels​

Easy·
FIG. NODES AT ODD LEVELS● INTERACTIVE
visualization loads as you reach it
def nodesAtOddLevels(self, root):
queue = collections.deque([root])
results = []
level = 1
while queue:
for _ in range(len(queue)):
node = queue.popleft()
if node:
if level % 2 == 1:
results.append(node.data)
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
level += 1
return sorted(results)

Next Right Node​

Easy·

Find the next right node of a given key in a binary tree. The next right node is the node that appears immediately to the right of the given key at the same level. If there's no such node, return a node with value -1.

3 Approaches
FIG. NEXT RIGHT NODE● INTERACTIVE
visualization loads as you reach it
def nextRight(self, root, key):
queue = collections.deque([root])
while queue:
length = len(queue)
for i in range(length):
node = queue.popleft()
if node:
if node.data == key:
return queue[0] if queue and (i != length - 1) else Node(-1)
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
return Node(-1)

1609. Even Odd Tree​

Medium·

A binary tree is named Even-Odd if it meets the following conditions:

  • The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc.
  • For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
  • For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).

Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.

3 Approaches
FIG. EVEN ODD TREE● INTERACTIVE
visualization loads as you reach it
def isEvenOddTree(self, root: Optional[TreeNode]) -> bool:
queue = collections.deque([root])
level = 0
 
isEven = lambda i: i % 2 == 0
notLastNode = lambda i: queue and i != length - 1
 
while queue:
length = len(queue)
for i in range(length):
node = queue.popleft()
if node:
if isEven(level):
if isEven(node.val) or (notLastNode(i) and node.val >= queue[0].val):
return False
else:
if not isEven(node.val) or (notLastNode(i) and node.val <= queue[0].val):
return False
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
level += 1
return True

513. Find Bottom Left Tree Value​

Medium·

Given the root of a binary tree, return the leftmost value in the last row of the tree.

3 Approaches
FIG. FIND BOTTOM LEFT TREE VALUE● INTERACTIVE
visualization loads as you reach it
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
queue = collections.deque([root])
leftmost = None
while queue:
for i in range(len(queue)):
node = queue.popleft()
if node:
if i == 0:
leftmost = node.val if node else None
queue.append(node.left) if node.left else None
queue.append(node.right) if node.right else None
return leftmost