Step through the algorithm visually — use Play or the step buttons (inspired by AlgoMaster / visualgo).
Airbnb interview context: Maximum Depth of Binary Tree is a Easy Trees problem — DFS (recursion/stack) or BFS (queue); clarify preorder/inorder/postorder.
Use the animation above to step through each move before writing code.
Pattern: Trees
Read from stdin, write to stdout. Classic interview problem #104.
Maximum Depth of Binary Tree — Airbnb interview prep · Trees
Classic interview problem #104.
Input (stdin)
Line 1: level-order values (-1 for null)
Output (stdout)
Maximum depth
Your program must read from stdin and write the answer to stdout (no extra debug text).
3 9 20 -1 -1 15 7
3
| Test | Status | Details |
|---|
Ready — edit the code above and click Run or Submit.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static int[] Ria(string line = null)
{
line ??= Console.ReadLine();
if (string.IsNullOrWhiteSpace(line)) return Array.Empty<int>();
return line.Trim().Split(new[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse).ToArray();
}
static string[] Rsa()
{
int n = int.Parse(Console.ReadLine());
var arr = new string[n];
for (int i = 0; i < n; i++) arr[i] = Console.ReadLine();
return arr;
}
static void W(params object[] parts) => Console.WriteLine(string.Join(" ", parts));
static void Wb(bool v) => Console.WriteLine(v ? "true" : "false");
static void Wi(int v) => Console.WriteLine(v);
static void Ws(string v) => Console.WriteLine(v);
static void Main()
{
var level = Ria();
int depth = 0, i = 0, width = 1;
while (i < level.Length) {
depth++;
i += width;
width *= 2;
}
Wi(depth);
}
}
Try solving on your own first, then reveal the official answer.