Ctrl K

Evenly Grown Tree

Easy
You are given the root of a binary tree. The height of a subtree is the number of nodes on the longest path from its top node down to a leaf; an empty subtree has height 0. Call a tree evenly grown if at **every** node the heights of the left and right subtrees differ by at most 1. Return True if the tree is evenly grown and False otherwise.
Example 1
root=tree_node([4, 2, 9, 1, None, 7, 11]) → True

Node 2 has a left subtree of height 1 and nothing on the right, a difference of 1, which is allowed. Both branches of the root have height 2.

Example 2
root=tree_node([6, 3, None, 2, None, 1]) → False

At the root 6 the left side is the chain 3 → 2 → 1 of height 3 and the right side is empty. A difference of 3 means the tree is lopsided.

Example 3
root=tree_node([]) → True

An empty tree has no node that could break the rule.

Constraints
  • 0 ≤ number of nodes ≤ 5000
  • -10⁴ ≤ Node.val ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLeft, Node, Right EasyIdentical Trees EasyMirror-Image Tree EasyTree Height EasyNearest Leaf Depth Easy
loading Python…
Input
root=tree_node([4, 2, 9, 1, None, 7, 11])
Expected
True

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