在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a rooted binary tree, find the lowest common ancestor of its deepest leaves. Recall that:
Example 1: Input: root = [1,2,3] Output: [1,2,3] Example 2: Input: root = [1,2,3,4] Output: [4] Example 3: Input: root = [1,2,3,4,5] Output: [2,4,5] Constraints:
给你一个有根节点的二叉树,找到它最深的叶节点的最近公共祖先。 回想一下:
示例 1: 输入:root = [1,2,3] 输出:[1,2,3] 示例 2: 输入:root = [1,2,3,4] 输出:[4] 示例 3: 输入:root = [1,2,3,4,5] 输出:[2,4,5] 提示:
40ms
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 lcaDeepestLeaves(_ root: TreeNode?) -> TreeNode? { 16 return _lcaDeepestLeaves(root).0 17 } 18 19 func _lcaDeepestLeaves(_ root: TreeNode?) -> (TreeNode?, Int) { 20 guard let root = root else { return (nil, 0) } 21 22 if root.left == nil && root.right == nil { 23 return (root, 1) 24 } else if root.left == nil { 25 let right = _lcaDeepestLeaves(root.right) 26 return (right.0, right.1 + 1) 27 } else if root.right == nil { 28 let left = _lcaDeepestLeaves(root.left) 29 return (left.0, left.1 + 1) 30 } else { 31 let left = _lcaDeepestLeaves(root.left) 32 let right = _lcaDeepestLeaves(root.right) 33 34 if left.1 > right.1 { 35 return (left.0, left.1 + 1) 36 } else if right.1 > left.1 { 37 return (right.0, right.1 + 1) 38 } else { 39 return (root, left.1 + 1) 40 } 41 } 42 } 43 } 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 lcaDeepestLeaves(_ root: TreeNode?) -> TreeNode? { 16 return helper(root).1 17 } 18 19 func helper(_ root: TreeNode?) -> (Int, TreeNode?) { 20 if root == nil { 21 return (0, root) 22 } 23 24 let left = helper(root!.left) 25 let right = helper(root!.right) 26 if left.0 > right.0 { 27 return (left.0+1, left.1) 28 } else if left.0 < right.0 { 29 return (right.0+1, right.1) 30 } else { 31 return (left.0+1, root) 32 } 33 } 34 } Runtime: 48 ms Memory Usage: 21.1 MB
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 DP:Int = 0 16 func lcaDeepestLeaves(_ root: TreeNode?) -> TreeNode? { 17 DP = dfs(root); 18 return dfs2(root, 0).0 19 } 20 21 func dfs(_ root: TreeNode?) -> Int 22 { 23 var ans:Int = 0 24 if root?.left != nil 25 { 26 ans = max(ans, 1 + dfs(root?.left)) 27 } 28 if root?.right != nil 29 { 30 ans = max(ans, 1 + dfs(root?.right)) 31 } 32 return ans 33 } 34 35 func dfs2(_ root: TreeNode?,_ dp:Int) -> (TreeNode?,Bool) 36 { 37 var p1:(TreeNode?,Bool) = (root,false) 38 var p2:(TreeNode?,Bool) = (root,false) 39 if root?.left != nil 40 { 41 p1 = dfs2(root!.left, dp + 1) 42 } 43 if root?.right != nil 44 { 45 p2 = dfs2(root!.right, dp + 1) 46 } 47 if p1.1 48 { 49 if p2.1 50 { 51 return (root,true) 52 } 53 else 54 { 55 return p1 56 } 57 } 58 else 59 { 60 if p2.1 {return p2} 61 else 62 { 63 if dp == DP 64 { 65 return (root, true) 66 } 67 else 68 { 69 return (root, false) 70 } 71 } 72 } 73 } 74 } 52ms
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 res: TreeNode? 16 var sz = [Int](repeating: 0, count: 1020) 17 var dep = [Int](repeating: 0, count: 1020) 18 var mxd = 0 19 func dfs(_ x: TreeNode, _ d: Int) { 20 sz[x.val] = 1 21 dep[x.val] = d 22 mxd = max(mxd, d) 23 if x.left != nil { 24 dfs(x.left!, d+1) 25 sz[x.val] += sz[x.left!.val] 26 } 27 28 if x.right != nil { 29 dfs(x.right!, d+1) 30 sz[x.val] += sz[x.right!.val] 31 } 32 } 33 34 func dfs2(_ x: TreeNode) -> Int{ 35 36 if dep[x.val] == mxd { 37 res = x 38 return x.val 39 } 40 var le = -1 41 var ri = -1; 42 if x.left != nil { 43 le = dfs2(x.left!) 44 } 45 if x.right != nil { 46 ri = dfs2(x.right!) 47 } 48 if le != -1 && ri != -1 { 49 res = x 50 } 51 return max(le, ri) 52 } 53 54 func lcaDeepestLeaves(_ root: TreeNode?) -> TreeNode? { 55 dfs(root!, 0) 56 dfs2(root!) 57 return res 58 } 59 } 64ms 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 extension TreeNode: Hashable { 15 public static func == (lhs: TreeNode, rhs: TreeNode) -> Bool { 16 return lhs === rhs 17 } 18 public func hash(into hasher: inout Hasher) { 19 hasher.combine(self.val) 20 } 21 } 22 23 class Solution { 24 typealias TreeNodeInfo = (parent: TreeNode?, level: Int) 25 var info = Dictionary<TreeNode?,TreeNodeInfo>() 26 var maxLevel = 0 27 func traverse(root: TreeNode?, level: Int) { 28 if root == nil { 29 return 30 } 31 let tempLexel = level + 1 32 if nil != root?.left { 33 if tempLexel > maxLevel { 34 maxLevel = tempLexel 35 } 36 info[root?.left] = (root, tempLexel) 37 traverse(root: root?.left, level: tempLexel) 38 } 39 if nil != root?.right { 40 if tempLexel > maxLevel { 41 maxLevel = tempLexel 42 } 43 info[root?.right] = (root, tempLexel) 44 traverse(root: root?.right, level: tempLexel) 45 } 46 } 47 func lcaDeepestLeaves(_ root: TreeNode?) -> TreeNode? { 48 if root == nil { 49 return nil 50 } 51 traverse(root: root, level: 0) 52 info[root] = (nil,0) 53 let depestLeaves = info.filter { (arg0) -> Bool in 54 let (_, value) = arg0 55 return value.level == maxLevel 56 } 57 if depestLeaves.count == 1{ 58 return depestLeaves.first!.key 59 } 60 var currentSet = Set<TreeNode?>(depestLeaves.keys) 61 while currentSet.count > 1 { 62 63 var parents = Set<TreeNode?>() 64 for node in currentSet { 65 parents.insert(info[node]?.parent) 66 } 67 currentSet = parents 68 } 69 return currentSet.first! 70 } 71 } 76ms 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 lcaDeepestLeaves(_ root: TreeNode?) -> TreeNode? { 16 17 guard let rootNode = root, rootNode.left != nil || rootNode.right != nil else { return root } 18 19 var stack = [[TreeNode]]() 20 var currentRow = [TreeNode]() 21 var nextRow = [rootNode] 22 23 var parents = [Int: TreeNode]() 24 25 repeat { 26 currentRow = nextRow 27 nextRow = [] 28 stack.append(currentRow) 29 30 for node in currentRow { 31 let children = [node.left, node.right].compactMap { $0 } 32 if let left = node.left { 33 parents[left.val] = node 34 } 35 if let right = node.right { 36 parents[right.val] = node 37 } 38 nextRow.append(contentsOf: children) 39 } 40 41 } while nextRow.count > 0 42 43 var deepestLeaves = stack.popLast()! 44 var deepestLeavesSet = Set<Int>(deepestLeaves.compactMap { $0.val }) 45 46 while deepestLeavesSet.count > 1 { 47 deepestLeaves = deepestLeaves.compactMap { parents[$0.val] } 48 deepestLeavesSet = Set<Int>(deepestLeaves.map { $0.val }) 49 } 50 51 return deepestLeaves.first! 52 } 53 }
|
请发表评论