• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode563.二叉树的坡度|BinaryTreeTilt

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift is Open Source 博客note发布时间:2022-07-14
下一篇:
iOS 多国语言本地化与App内语言切换(Swift)发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap