在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a binary search tree and the lowest and highest boundaries as Example 1: Input: 1 / \ 0 2 L = 1 R = 2 Output: 1 \ 2 Example 2: Input: 3 / \ 0 4 \ 2 / 1 L = 1 R = 3 Output: 3 / 2 / 1 给定一个二叉搜索树,同时给定最小边界 示例 1: 输入: 1 / \ 0 2 L = 1 R = 2 输出: 1 \ 2 示例 2: 输入: 3 / \ 0 4 \ 2 / 1 L = 1 R = 3 输出: 3 / 2 / 1 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 func trimBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> TreeNode? { 16 guard let r = root else { return nil } 17 18 if r.val > R { return trimBST(r.left, L, R) } 19 if r.val < L { return trimBST(r.right, L, R) } 20 21 r.left = trimBST(r.left, L, R) 22 r.right = trimBST(r.right, L, R) 23 24 return r 25 } 26 } 84ms 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 trimBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> TreeNode? { 16 guard let root = root else { 17 return nil 18 } 19 20 var resultTree: TreeNode? = nil 21 preorderTraversal(root) { 22 val in 23 if val >= L && val <= R { 24 resultTree = addNode(resultTree, val) 25 } 26 } 27 return resultTree 28 } 29 } 30 31 func addNode(_ root: TreeNode?, _ val: Int) -> TreeNode { 32 guard let root = root else { 33 return TreeNode(val) 34 } 35 36 if val <= root.val { 37 root.left = addNode(root.left, val) 38 } else { 39 root.right = addNode(root.right, val) 40 } 41 return root 42 } 43 44 func preorderTraversal(_ root: TreeNode?, _ visit: (Int) -> Void) { 45 guard let root = root else { 46 return 47 } 48 visit(root.val) 49 preorderTraversal(root.left, visit) 50 preorderTraversal(root.right, visit) 51 } 96ms 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 trimBST(_ root: TreeNode?, _ L: Int, _ R: Int) -> TreeNode? { 16 guard let r = root else { 17 return nil 18 } 19 if r.val < L { 20 return trimBST(r.right, L, R) 21 } else if r.val > R { 22 return trimBST(r.left, L, R) 23 } 24 r.left = trimBST(r.left, L, R) 25 r.right = trimBST(r.right, L, R) 26 return r 27 } 28 29 func deleteNode(_ node: TreeNode) -> TreeNode?{ 30 if node.left == nil { 31 return node.right 32 } else if node.right == nil { 33 return node.left 34 } else { 35 return insert(root: node.left, node: node.right!) 36 } 37 } 38 39 func insert(root: TreeNode?, node: TreeNode) -> TreeNode { 40 guard var n = root else { 41 return node 42 } 43 44 if node.val < n.val { 45 if n.left == nil { 46 n.left = node 47 } else { 48 n.left = insert(root: n.left, node: node) 49 } 50 } else { 51 if n.right == nil { 52 n.right = node 53 } else { 54 n.right = insert(root: n.right, node: node) 55 } 56 } 57 58 return node 59 } 60 }
|
请发表评论