[Swift]LeetCode563. 二叉樹的坡度 | Binary Tree Tilt
阿新 • • 發佈:2018-10-24
define ltr hole swift not all div pla integer
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:
- The sum of node values in any subtree won‘t exceed the range of 32-bit integer.
- 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
註意:
- 任何子樹的結點的和不會超過32位整數的範圍。
- 坡度的值不會超過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 }
[Swift]LeetCode563. 二叉樹的坡度 | Binary Tree Tilt