Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - Найти одинаковые поддеревья
- // 625
- func findDuplicateSubtrees(_ root: TreeNode?) -> [TreeNode?] {
- var hashMap = [Int: Bool]()
- var result = [TreeNode?]()
- func dfs(_ node: TreeNode?) -> Int {
- guard let node = node else { return 0 }
- let hashValue = [node.val, dfs(node.left), dfs(node.right)].hashValue
- if hashMap[hashValue] == true {
- result.append(node)
- }
- hashMap[hashValue] = hashMap[hashValue] == nil
- return hashValue
- }
- dfs(root)
- return result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement