How Many Levels Tall Is the Tree

Implement treeHeight

You are given the root of a binary tree. Return its height: the number of nodes on the longest path that starts at the root and goes down to a leaf. An empty tree has height 0.

You can carry the current depth downward and remember the largest one you see, or let each call return the height of its own subtree and combine the two children with a max.

Example 1:

Input: root = [11,6,15,3,8,null,20,1]

Output: 4

Example 2:

Input: root = [4,null,9,null,2]

Output: 3

Example 3:

Input: root = []

Output: 0

+ 11 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ number of nodes ≤ 100
  • ●−100 ≤ node.val ≤ 100 (the values never affect the answer)
  • ●The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child
  • ●Return the number of nodes on the longest downward path from the root to a leaf (the number of levels). An empty tree has 0 levels

root =

[11, 6, 15, 3, 8, null, 20, 1]