1036. Height of Special Binary Tree
The root of a special binary tree with n nodes is provided to you. The special binary tree's nodes are assigned numbers ranging from 1 to n. Assume that the tree's k leaves are arranged as follows: b1 < b2 <... < bk.
This tree's leaves have a unique quality! In other words, the following circumstances apply to each leaf bi:
- If i is less than k, the right child of bi is bi + 1, and if not, it is b1.
- If i > 1, the left child of bi is bi - 1; if not, it is bk.
Provide the tree's height in return.Â
Note: The height of a binary tree is the length of the longest path from the root to any other node.
Example 1:
Input : root = [1,2,3,null,null,4,5]
Output : 2
Explanation :
The given tree is shown in the following picture. Each leaf's left child is the leaf to its left (shown with the blue edges). Each leaf's right child is the leaf to its right (shown with the red edges). We can see that the graph has a height of 2.
Example 2:
Input : root = [1,2,3,null,null,4,null,5,6]
Output : 3
Explanation :
The given tree is shown in the following picture. Each leaf's left child is the leaf to its left (shown with the blue edges). Each leaf's right child is the leaf to its right (shown with the red edges). We can see that the graph has a height of 3.
Now Your Turn!
Pick the correct output for the given inputInput : root = [1, 2, 3, 4, null, 5, 6]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == number of nodes in the tree
- 2 <= n <= 104
- 1 <= node.val <= n
- The input is generated such that each node.val is unique.