★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qnxfxvqj-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a binary tree, return the tilt of the whole tree.node
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.git
The tilt of the whole tree is defined as the sum of all nodes' tilt.github
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:spa
給定一個二叉樹,計算整個樹的坡度。code
一個樹的節點的坡度定義即爲,該節點左子樹的結點之和和右子樹結點之和的差的絕對值。空結點的的坡度是0。htm
整個樹的坡度就是其全部節點的坡度之和。blog
示例:get
輸入: 1 / \ 2 3 輸出: 1 解釋: 結點的坡度 2 : 0 結點的坡度 3 : 0 結點的坡度 1 : |2-3| = 1 樹的坡度 : 0 + 0 + 1 = 1
注意:
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 }