...

Chapter 16 - Python Quest

Binary Trees

Measure branches, traverse by depth and level, then search and grow binary search trees.

XP
0Level 1
Stars
0/30quest stars
Combo
0clean runs
Rank
Seed0/160 Python score
Recursive binary-tree measurements

Treehouse Canopy Height

100 XP

A treehouse architect counts how many node platforms appear on the longest path from the root platform down to a leaf.

Write tree_height(root) so it returns the number of nodes on the longest root-to-leaf path. An empty tree has height 0. The judge gives root as a TreeNode with value, left, and right attributes.

Sample checks

tree_height(root)returns3
root
root: 88root-left: 44root-right: 1212root-left-left: 22root-left-right: 66root-right-left: 1010root-right-right: 1414

Explanation: the longest paths are 8 to 4 to either 2 or 6 and 8 to 12 to either 10 or 14, each containing three node platforms

tree_height(root)returns0
root
Empty tree (root = None)

Explanation: the level-order drawing is empty, so root is None and there is no node platform on any path

Hint

Hints are ready when you want one.

Lesson reference

Review: Ask the same question of both branches

Your Python

tree_height

Loading editor

Judge

Ready

0/5

Run your code when it feels ready.