[Swift] LeetCode1161 elements and the inner layer of the maximum |. Maximum Level Sum of a Binary Tree

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: to dare (WeiGanTechnologies)
➤ blog Park address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: HTTPS: //www.cnblogs. com / strengthen / p / 11371957.html 
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.

Return the smallest level X such that the sum of all the values of nodes at level X is maximal.

Example 1:

Input: [1,7,0,7,-8,null,null]
Output: 2
Explanation: 
Level 1 sum = 1.
Level 2 sum = 7 + 0 = 7.
Level 3 sum = 7 + -8 = -1.
So we return the level with the maximum sum which is level 2.

Note:

  1. The number of nodes in the given tree is between 1 and 10^4.
  2. -10^5 <= node.val <= 10^5

Give you the root node of a binary tree  root. Located in the root of the binary tree is provided  1 layer, while the child nodes of the root located on the  2 floor, and so on.

Please identify the elements of the inner layer and the largest of the several layers (probably only one) of the layer number, and returns the smallest one.

Example:

Input: [1,7,0,7, -8, null, null] 
Output: 2 
Explanation: 
The first layer of each element is the sum of 1, 
the second layer of each element is the sum of 7 + 0 = 7, 
the third layer the elements and -8 to + 7 = -1, 
so we return layer number of the second layer, the inner layer element it is the sum of the maximum.

prompt:

  1. Tree nodes between  1 and  10^4 among
  2. -10^5 <= node.val <= 10^5

Guess you like

Origin www.cnblogs.com/strengthen/p/11371957.html