From c5dfcc54f3fe5c1405876f5b17449d8797a50275 Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Thu, 24 Oct 2024 19:52:16 +0900 Subject: [PATCH 1/2] https://leetcode.com/problems/flip-equivalent-binary-trees --- medium/flip_equivalent_binary_trees.py | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 medium/flip_equivalent_binary_trees.py diff --git a/medium/flip_equivalent_binary_trees.py b/medium/flip_equivalent_binary_trees.py new file mode 100644 index 0000000..87a3bdd --- /dev/null +++ b/medium/flip_equivalent_binary_trees.py @@ -0,0 +1,35 @@ +from typing import Optional + +# https://leetcode.com/problems/flip-equivalent-binary-trees +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def flipEquiv(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: + if root1 == None and root2 == None: + return True + if root1 == None or root2 == None: + return False + if root1.val != root2.val: + return False + flip = False + if ( + (root1.left == None and root2.right == None) or \ + (root1.left != None and root2.right != None and \ + root1.left.val == root2.right.val) + ) and \ + ( + (root1.right == None and root2.left == None) or \ + (root1.right != None and root2.left != None and \ + root1.right.val == root2.left.val) + ): + flip = True + if flip: + tmp = root2.left + root2.left = root2.right + root2.right = tmp + return self.flipEquiv(root1.left, root2.left) and \ + self.flipEquiv(root1.right, root2.right) From d161361b65ff65a1fca87813bbfe1f011078b1e3 Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Sat, 26 Oct 2024 00:02:41 +0900 Subject: [PATCH 2/2] https://leetcode.com/problems/remove-sub-folders-from-the-filesystem --- .../remove_sub_folders_from_the_filesystem.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 medium/remove_sub_folders_from_the_filesystem.py diff --git a/medium/remove_sub_folders_from_the_filesystem.py b/medium/remove_sub_folders_from_the_filesystem.py new file mode 100644 index 0000000..9f54a82 --- /dev/null +++ b/medium/remove_sub_folders_from_the_filesystem.py @@ -0,0 +1,30 @@ +# https://leetcode.com/problems/remove-sub-folders-from-the-filesystem +from typing import List +import copy + +class Solution: + def removeSubfolders(self, folder: List[str]) -> List[str]: + trie = {} + def push(trie, words, index): + if index >= len(words): + trie['end'] = True + return + if words[index] not in trie: + trie[words[index]] = {} + return push(trie[words[index]], words, index + 1) + def pop(trie, curr, ret): + if trie == None: + return + if 'end' in trie: + ret.append('/' + '/'.join(curr)) + return + for k, v in trie.items(): + cur = copy.deepcopy(curr) + cur.append(k) + pop(v, cur, ret) + for each in folder: + words = each.split('/') + push(trie, words[1:], 0) + ret = [] + pop(trie, [], ret) + return ret