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 3Output: 1Explanation: Tilt of node 2 : 0Tilt of node 3 : 0Tilt of node 1 : |2-3| = 1Tilt 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 = nil10 * self.right = nil11 * }12 * }13 */14 class Solution {15 func findTilt(_ root: TreeNode?) -> Int {16 var sum = 017 18 func getAllTilts(_ root: TreeNode?) -> Int {19 guard let root = root else { return 0 }20 var lVal = 021 var rVal = 022 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.val30 }31 32 getAllTilts(root)33 return sum34 }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 = nil10 * self.right = nil11 * }12 * }13 */14 class Solution {15 var tilt = 016 17 func findTilt(_ root: TreeNode?) -> Int {18 aux(root)19 return tilt20 }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!.val31 }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 = nil10 * self.right = nil11 * }12 * }13 */14 class Solution {15 var tilt = 016 17 func findTilt(_ root: TreeNode?) -> Int {18 traverse(root)19 return tilt20 }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.val31 }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 = nil10 * self.right = nil11 * }12 * }13 */14 class Solution {15 var tilt = 016 func findTilt(_ root: TreeNode?) -> Int {17 18 helper(root) 19 return tilt20 }21 22 func helper(_ root: TreeNode?) -> Int {23 guard let root = root else {24 return 025 }26 27 let left = helper(root.left)28 let right = helper(root.right)29 tilt += abs(left - right)30 31 return root.val + left + right32 }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 = nil10 * self.right = nil11 * }12 * }13 */14 class Solution {15 16 var sum = 017 18 func findTilt(_ root: TreeNode?) -> Int {19 traverse(root)20 return sum21 }22 23 func traverse(_ root: TreeNode?) -> Int {24 guard let root = root else {25 return 026 }27 var left = traverse(root.left)28 var val = root.val29 let right = traverse(root.right)30 sum += Int(abs(left - right))31 return left + right + val32 }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 = nil10 * self.right = nil11 * }12 * }13 */14 class Solution {15 16 var sum = 017 18 func findTilt(_ root: TreeNode?) -> Int {19 traverse(root)20 return sum21 }22 23 func traverse(_ root: TreeNode?) -> Int {24 guard let root = root else {25 return 026 }27 var left = traverse(root.left)28 var val = root.val29 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 + val39 }40 }总结
以上是内存溢出为你收集整理的[Swift]LeetCode563. 二叉树的坡度 | Binary Tree Tilt全部内容,希望文章能够帮你解决[Swift]LeetCode563. 二叉树的坡度 | Binary Tree Tilt所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)