[Swift]LeetCode1080. 根到葉路徑上的不足節點 | Insufficient Nodes in Root to Leaf Paths

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

Given the root of a binary tree, consider all root to leaf paths: paths from the root to any leaf.  (A leaf is a node with no children.)node

node is insufficient if every such root to leaf path intersecting this node has sum strictly less than limit.微信

Delete all insufficient nodes simultaneously, and return the root of the resulting binary tree.less

 

Example 1:ide

Input: root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1  Output: [1,2,3,4,null,null,7,8,9,null,14] 

Example 2:this

Input: root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22  Output: [5,4,8,11,null,17,4,7,null,null,null,5] 

Note:spa

  1. The given tree will have between 1 and 5000 nodes.
  2. -10^5 <= node.val <= 10^5
  3. -10^9 <= limit <= 10^9

給定二叉樹的根 root,考慮全部從根到葉的路徑:從根到任何葉的路徑。 (葉節點是沒有子節點的節點。)code

若是交於節點 node 的每一個根到葉路徑的總和嚴格小於限制 limit,則該節點爲不足節點。htm

同時刪除全部不足節點,並返回生成的二叉樹的根。 blog

示例 1:

輸入:root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1

輸出:[1,2,3,4,null,null,7,8,9,null,14]

示例 2:

輸入:root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22

輸出:[5,4,8,11,null,17,4,7,null,null,null,5]

示例 3:

輸入:root = [5,-6,-6], limit = 0
輸出:[] 

提示:

  1. 給定的樹有 1 到 5000 個節點
  2. -10^5 <= node.val <= 10^5
  3. -10^9 <= limit <= 10^9

128ms
 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 sufficientSubset(_ root: TreeNode?, _ limit: Int) -> TreeNode? {
16         var newRoot = root
17         let sufficient = isSufficientSubset(&newRoot, limit, 0)
18         if !sufficient{
19             return nil
20         }
21         
22         return newRoot
23     }
24     
25     func isSufficientSubset(_ root:inout TreeNode?, _ limit: Int,_ currentSum:Int) -> Bool{
26         guard let root = root else{
27             return currentSum >= limit
28         }
29         
30         let leftSuff = isSufficientSubset(&root.left, limit, currentSum + root.val)
31         let rightSuff = isSufficientSubset(&root.right, limit, currentSum + root.val)
32         if !leftSuff {
33             root.left = nil
34         }
35         
36         if !rightSuff {
37             root.right = nil
38         }
39         
40         return leftSuff || rightSuff
41     }
42 }

132ms

 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 sufficientSubset(_ root: TreeNode?, _ limit: Int) -> TreeNode? {        
16         guard let root = root else { return nil }
17         if root.left === root.right {
18             return root.val < limit ? nil : root
19         }
20         if root.left != nil {
21             root.left = sufficientSubset(root.left, limit - root.val)
22         }
23         if root.right != nil {
24             root.right = sufficientSubset(root.right, limit - root.val)
25         }
26         return root.left === root.right ? nil : root
27     }
28 }

Runtime: 152 ms
Memory Usage: 21.4 MB
 1 class Solution {
 2     func sufficientSubset(_ root: TreeNode?, _ limit: Int) -> TreeNode? {
 3         let res:Pair = helper(root, 0, limit)
 4         return res.node
 5     }
 6     
 7     func helper(_ root: TreeNode?,_ cur:Int, _ limit: Int) -> Pair
 8     {
 9         var left:Pair? = nil
10         var right:Pair? = nil
11         
12         if root != nil && root?.left == nil && root?.right == nil
13         {
14             if  cur + root!.val < limit
15             {
16                 return Pair(nil, root!.val)
17             }
18             else
19             {
20                 return Pair(root, root!.val)
21             }
22         }
23         
24         var max_val = Int.min
25         if root?.left != nil
26         {
27             left = helper(root?.left, cur + root!.val, limit)
28             max_val = max(left!.max_val, max_val)
29         }
30         
31         if root?.right != nil
32         {
33             right = helper(root?.right, cur + root!.val, limit)
34             max_val = max(right!.max_val, max_val)
35         }
36         
37         if left != nil && left?.node == nil
38         {
39             root?.left = nil
40         }
41         if right != nil && right?.node == nil
42         {
43             root?.right = nil
44         }
45         
46         if (max_val + root!.val + cur < limit)
47         {
48             return Pair(nil, max_val + root!.val);
49         }
50         else
51         {
52             return Pair(root, max_val + root!.val)
53         }
54     }
55 }
56 
57 class Pair
58 {
59     var node:TreeNode?
60     var max_val:Int
61     
62     init(_ n:TreeNode?,_ v:Int)
63     {
64         self.node = n
65         self.max_val = v
66     }
67 }
相關文章
相關標籤/搜索