Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.
The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null
nodes between the end-nodes are also counted into the length calculation.
Example 1:
Input: 1 / \ 3 2 / \ \ 5 3 9 Output: 4 Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
Example 2:
Input: 1 / 3 / \ 5 3 Output: 2 Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:
Input: 1 / \ 3 2 / 5 Output: 2 Explanation: The maximum width existing in the second level with the length 2 (3,2).
Example 4:
Input: 1 / \ 3 2 / \ 5 9 / \ 6 7 Output: 8 Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
Note: Answer will in the range of 32-bit signed integer.
给定一个二叉树,编写一个函数来获取这个树的最大宽度。树的宽度是所有层中的最大宽度。这个二叉树与满二叉树(full binary tree)结构相同,但一些节点为空。
每一层的宽度被定义为两个端点(该层最左和最右的非空节点,两端点间的null
节点也计入长度)之间的长度。
示例 1:
输入: 1 / \ 3 2 / \ \ 5 3 9 输出: 4 解释: 最大值出现在树的第 3 层,宽度为 4 (5,3,null,9)。
示例 2:
输入: 1 / 3 / \ 5 3 输出: 2 解释: 最大值出现在树的第 3 层,宽度为 2 (5,3)。
示例 3:
输入: 1 / \ 3 2 / 5 输出: 2 解释: 最大值出现在树的第 2 层,宽度为 2 (3,2)。
示例 4:
输入: 1 / \ 3 2 / \ 5 9 / \ 6 7 输出: 8 解释: 最大值出现在树的第 4 层,宽度为 8 (6,null,null,null,null,null,null,7)。
注意: 答案在32位有符号整数的表示范围内。
ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 var map:[Int:Int] = [Int:Int]() 16 var res:Int = 0 17 func widthOfBinaryTree(_ root: TreeNode?) -> Int { 18 helper(root, 0, 1); 19 return res; 20 } 21 22 func helper(_ node:TreeNode?,_ depth:Int,_ p:Int) 23 { 24 if node == nil {return} 25 if map[depth] == nil 26 { 27 map[depth] = p 28 res = max(res, 1) 29 } 30 else 31 { 32 res = max(res, p - map[depth,default:0] + 1) 33 } 34 helper(node?.left, depth + 1, p * 2) 35 helper(node?.right, depth + 1, p * 2 + 1) 36 } 37 }
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func widthOfBinaryTree(_ root: TreeNode?) -> Int { 16 var res:Int = 0 17 var start:[Int] = [Int]() 18 helper(root, 0, 1, &start, &res) 19 return res 20 } 21 22 func helper(_ node: TreeNode?,_ h:Int,_ idx:Int,_ start:inout [Int],_ res:inout Int) 23 { 24 if node == nil {return} 25 if h >= start.count 26 { 27 start.append(idx) 28 } 29 res = max(res, idx - start[h] + 1) 30 helper(node?.left, h + 1, idx * 2, &start, &res) 31 helper(node?.right, h + 1, idx * 2 + 1, &start,&res) 32 } 33 }
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func widthOfBinaryTree(_ root: TreeNode?) -> Int { 16 var start:[Int] = [Int]() 17 return helper(root, 0, 1, &start) 18 } 19 20 func helper(_ node: TreeNode?,_ h:Int,_ idx:Int,_ start:inout [Int]) -> Int 21 { 22 if node == nil {return 0} 23 if h >= start.count 24 { 25 start.append(idx) 26 } 27 return 28 max(idx - start[h] + 1, helper(node!.left, h + 1, idx * 2, &start), helper(node!.right, h + 1, idx * 2 + 1, &start)) 29 } 30 }