Algorithm

Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
Input: root = [5,1,7]
Output: [1,null,5,null,7]
  • The number of nodes in the given tree will be in the range [1, 100].
  • 0 <= Node.val <= 1000
Input: root = [3,9,20,null,null,15,7]
Output: 3
Input: root = [1,null,2]
Output: 2
Input: root = []
Output: 0
Input: root = [0]
Output: 1
  • The number of nodes in the tree is in the range [0, 104].
  • -100 <= Node.val <= 100
Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
Input: n = 25
Output: 1389537
  • 0 <= n <= 37
  • The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store