Skip to the code
cf.
+10 XP

#316 Deepest Split Pane

easy

A code editor stores its window layout as a binary tree. Each node is a pane, and splitting a pane gives it up to two child panes, left and right.

Write maxNesting(root) that returns how many panes are on the longest path from the root pane down to a pane with no children. An empty layout (root is null) returns 0.

The examples show the tree in level order, with null for a missing child.

Example 1

Input: root = [1,2,3,4,5]
Output: 3

The path 1 → 2 → 4 has three panes.

Example 2

Input: root = [1,null,2,null,3]
Output: 3

Each pane has only a right child, so all three are on one path.

Constraints

  • 0 <= number of panes <= 10000
  • Pane ids are whole numbers.

Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).