志,敢教日月换新天。为有牺牲多壮

[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 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 之间。
  • 每个结点的值都是独一无二的。

Runtime: 16 ms
Memory Usage: 19 MB
复制代码
 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 }
复制代码

 

posted @   为敢技术  阅读(285)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°