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

[Swift]LeetCode563. 二叉树的坡度 | Binary Tree Tilt

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9842592.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

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

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

Given a binary tree, return the tilt of the whole tree.

The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0.

The tilt of the whole tree is defined as the sum of all nodes' tilt.

Example:

Input: 
         1
       /   \
      2     3
Output: 1
Explanation: 
Tilt of node 2 : 0
Tilt of node 3 : 0
Tilt of node 1 : |2-3| = 1
Tilt of binary tree : 0 + 0 + 1 = 1

 Note:

  1. The sum of node values in any subtree won't exceed the range of 32-bit integer.
  2. All the tilt values won't exceed the range of 32-bit integer.

给定一个二叉树,计算整个树的坡度。

一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值。空结点的的坡度是0。

整个树的坡度就是其所有节点的坡度之和。

示例:

输入: 
         1
       /   \
      2     3
输出: 1
解释: 
结点的坡度 2 : 0
结点的坡度 3 : 0
结点的坡度 1 : |2-3| = 1
树的坡度 : 0 + 0 + 1 = 1

注意:

  1. 任何子树的结点的和不会超过32位整数的范围。
  2. 坡度的值不会超过32位整数的范围。

44ms

 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 findTilt(_ root: TreeNode?) -> Int {
16         var sum = 0
17         
18         func getAllTilts(_ root: TreeNode?) -> Int {
19             guard let root = root else { return 0 }
20             var lVal = 0
21             var rVal = 0
22             if root.left != nil {
23                 lVal = getAllTilts(root.left)
24             }
25             if root.right != nil {
26                 rVal = getAllTilts(root.right)
27             }
28             sum += abs(lVal - rVal)
29             return lVal + rVal + root.val
30         }
31         
32         getAllTilts(root)
33         return sum
34     }
35 }

48ms

 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 tilt = 0
16     
17     func findTilt(_ root: TreeNode?) -> Int {
18         aux(root)
19         return tilt
20     }
21     func aux(_ root:TreeNode?) -> Int {
22         if root == nil {
23             return 0 
24         }
25         let tiltLeft = aux(root?.left)
26         let tiltRight = aux(root?.right)
27         
28         tilt += abs(tiltLeft - tiltRight) 
29         
30         return tiltLeft + tiltRight + root!.val
31     }
32 }

68ms

 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 tilt = 0
16     
17     func findTilt(_ root: TreeNode?) -> Int {
18         traverse(root)
19         return tilt
20     }
21     
22     func traverse(_ root: TreeNode?) -> Int {
23         guard let root = root else { return 0 }
24         
25         let l = traverse(root.left)
26         let r = traverse(root.right)
27         
28         tilt += abs(l - r)
29         
30         return l + r + root.val
31     }
32 }

88ms

 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 tilt = 0
16     func findTilt(_ root: TreeNode?) -> Int {
17                 
18         helper(root)       
19         return  tilt
20     }
21     
22     func helper(_ root: TreeNode?) -> Int {
23         guard let root = root else {
24             return 0
25         }
26         
27         let left = helper(root.left)
28         let right = helper(root.right)
29         tilt +=  abs(left - right)
30         
31         return root.val + left + right
32     }
33 }

92ms

 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     
16     var sum = 0
17 
18 func findTilt(_ root: TreeNode?) -> Int {
19         traverse(root)
20         return sum
21     }
22     
23     func traverse(_ root: TreeNode?) -> Int {
24         guard let root = root else {
25             return 0
26         }
27         var left = traverse(root.left)
28         var val = root.val
29         let right = traverse(root.right)
30         sum += Int(abs(left - right))
31         return left + right + val
32     }
33 }

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     
16     var sum = 0
17 
18 func findTilt(_ root: TreeNode?) -> Int {
19         traverse(root)
20         return sum
21     }
22     
23     func traverse(_ root: TreeNode?) -> Int {
24         guard let root = root else {
25             return 0
26         }
27         var left = traverse(root.left)
28         var val = root.val
29         let right = traverse(root.right)
30         if (val == 1) {
31             print(left)
32             print(abs(left - right))
33         }
34         sum += Int(abs(left - right))
35         if (val == 2) {
36             print(left + right + val)
37         }
38         return left + right + val
39     }
40 }

 

posted @ 2018-10-24 11:30  为敢技术  阅读(310)  评论(0编辑  收藏  举报