[Swift]LeetCode865. 具有所有最深结点的最小子树 | Smallest Subtree with all the Deepest Nodes
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10596780.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a binary tree rooted at root
, the depth of each node is the shortest distance to the root.
A node is deepest if it has the largest depth possible among any node in the entire tree.
The subtree of a node is that node, plus the set of all descendants of that node.
Return the node with the largest depth such that it contains all the deepest nodes in its subtree.
Example 1:Input: [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation:
We return the node with value 2, colored in yellow in the diagram.
The nodes colored in blue are the deepest nodes of the tree.
The input "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" is a serialization of the given tree.
The output "[2, 7, 4]" is a serialization of the subtree rooted at the node with value 2.
Both the input and output have TreeNode type.
Note:
- The number of nodes in the tree will be between 1 and 500.
- The values of each node are unique.
给定一个根为 root
的二叉树,每个结点的深度是它到根的最短距离。
如果一个结点在整个树的任意结点之间具有最大的深度,则该结点是最深的。
一个结点的子树是该结点加上它的所有后代的集合。
返回能满足“以该结点为根的子树中包含所有最深的结点”这一条件的具有最大深度的结点。
示例:
输入:[3,5,1,6,2,0,8,null,null,7,4] 输出:[2,7,4] 解释:
我们返回值为 2 的结点,在图中用黄色标记。 在图中用蓝色标记的是树的最深的结点。 输入 "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" 是对给定的树的序列化表述。 输出 "[2, 7, 4]" 是对根结点的值为 2 的子树的序列化表述。 输入和输出都具有 TreeNode 类型。
提示:
- 树中结点的数量介于 1 和 500 之间。
- 每个结点的值都是独一无二的。
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 subtreeWithAllDeepest(_ root: TreeNode?) -> TreeNode? { 16 return deep(root).1 17 } 18 19 func deep(_ root: TreeNode?) -> (Int,TreeNode?) 20 { 21 if root == nil {return (0, nil)} 22 var l:(Int,TreeNode?) = deep(root?.left) 23 var r:(Int,TreeNode?) = deep(root?.right) 24 var d1:Int = l.0 25 var d2:Int = r.0 26 return (max(d1, d2) + 1, d1 == d2 ? root : d1 > d2 ? l.1 : r.1); 27 } 28 }
16ms
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 subtreeWithAllDeepest(_ root: TreeNode?) -> TreeNode? { 16 let (node, _) = helper(root, 0) 17 return node 18 } 19 20 private func helper(_ root: TreeNode?, _ level: Int) -> (TreeNode?, Int) { 21 guard root != nil else { 22 return (root, level) 23 } 24 let (leftNode, leftLevel) = helper(root?.left, level + 1) 25 let (rightNode, rightLevel) = helper(root?.right, level + 1) 26 if leftLevel == rightLevel { 27 return (root, leftLevel) 28 } else if leftLevel > rightLevel { 29 return (leftNode, leftLevel) 30 } else { 31 return (rightNode, rightLevel) 32 } 33 } 34 }
20ms
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 subtreeWithAllDeepest(_ root: TreeNode?) -> TreeNode? { 16 var paths = [[TreeNode]]() 17 var soFar = [TreeNode]() 18 var max = 0 19 getAllLongestPaths(root, &paths, &soFar, &max, 0) 20 if paths.count == 0 { return nil } 21 var result: TreeNode? = nil 22 for i in paths[0].indices { 23 var temp = paths[0][i] 24 var foundDiff = false 25 for j in 1..<paths.count { 26 if paths[j][i].val != temp.val { 27 foundDiff = true 28 break 29 } 30 } 31 if foundDiff { break } 32 result = temp 33 } 34 return result 35 } 36 37 func getAllLongestPaths(_ root: TreeNode?, _ path: inout [[TreeNode]], _ soFar: inout [TreeNode], 38 _ maxLevel: inout Int, _ level: Int) { 39 if (root == nil) { return } 40 41 if root!.left == nil && root!.right == nil { 42 if level >= maxLevel { 43 if level > maxLevel { 44 maxLevel = level 45 path.removeAll() 46 } 47 soFar.append(root!) 48 path.append(soFar) 49 soFar.removeLast() 50 } 51 return 52 } 53 maxLevel = max(maxLevel, level) 54 soFar.append(root!) 55 getAllLongestPaths(root!.left, &path, &soFar, &maxLevel, level + 1) 56 getAllLongestPaths(root!.right, &path, &soFar, &maxLevel, level + 1) 57 soFar.removeLast() 58 } 59 }
24ms
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 subtreeWithAllDeepest(_ root: TreeNode?) -> TreeNode? { 16 return subtree(root).node 17 } 18 19 func subtree(_ root: TreeNode?) -> (node: TreeNode?, depth: Int) { 20 guard let root = root else { 21 return (node: nil, depth: 0) 22 } 23 24 let left = subtree(root.left) 25 let right = subtree(root.right) 26 27 if left.depth > right.depth { 28 return (node: left.node, depth: left.depth + 1) 29 } 30 31 if left.depth < right.depth { 32 return (node: right.node, depth: right.depth + 1) 33 } 34 35 return (node: root, depth: left.depth + 1) 36 } 37 }