[Swift]LeetCode865. 具備全部最深結點的最小子樹 | Smallest Subtree with all the Deepest Nodes

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-vfdmwtip-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a binary tree rooted at root, the depth of each node is the shortest distance to the root.node

A node is deepest if it has the largest depth possible among any node in the entire tree.git

The subtree of a node is that node, plus the set of all descendants of that node.github

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]app

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:spa

  • The number of nodes in the tree will be between 1 and 500.
  • The values of each node are unique.

給定一個根爲 root 的二叉樹,每一個結點的深度是它到根的最短距離。code

若是一個結點在整個樹的任意結點之間具備最大的深度,則該結點是最深的。htm

一個結點的子樹是該結點加上它的全部後代的集合。blog

返回能知足「以該結點爲根的子樹中包含全部最深的結點」這一條件的具備最大深度的結點。 

示例:

輸入:[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 }
相關文章
相關標籤/搜索