Ctrl K

Tree Height

Easy
You are given the root of a binary tree, tree. Find its height: the number of nodes on the longest downward path from the root to any leaf, counting both the root and the leaf. An empty tree has height 0, and a single-node tree has height 1.
Example 1
tree=tree_node([5, 2, 8, None, 4, None, None, 3]) → 4

The longest branch is 5 → 2 → 4 → 3, which has four nodes.

Example 2
tree=tree_node([]) → 0

The tree is empty, so its height is 0.

Example 3
tree=tree_node([-7]) → 1
Constraints
  • 0 ≤ number of nodes ≤ 10⁴
  • -100 ≤ node value ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIdentical Trees EasyMirror-Image Tree EasyNearest Leaf Depth EasyLeaf Path With a Given Total EasyMirror the Tree Easy
loading Python…
Input
tree=tree_node([5, 2, 8, None, 4, None, None, 3])
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.