diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6f503d6711767..78d60046b044d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -46,7 +46,7 @@ jobs: python3 -m pip install --upgrade pip python3 -m pip install -r requirements.txt python3 -m pip install "mkdocs-material[imaging]" - apt-get install pngquant + sudo apt-get install pngquant - run: | python3 main.py @@ -73,4 +73,4 @@ jobs: gitee-username: yanglbme gitee-password: ${{ secrets.GITEE_PASSWORD }} gitee-repo: doocs/leetcode - branch: gh-pages \ No newline at end of file + branch: gh-pages diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md index 7d931442d6d9e..7f40a523c16c4 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md @@ -89,7 +89,7 @@ findElements.find(5); // return True ### 方法一:DFS + 哈希表 -我们先通过 DFS 遍历二叉树,将节点值恢复为原来的值,然后再通过哈希表存储所有节点值,这样在查找时就可以直接判断目标值是否存在于哈希表中。 +我们先通过 DFS 遍历二叉树,将节点值恢复为原来的值,并将所有节点值存入哈希表中。然后在查找时,只需要判断哈希表中是否存在目标值即可。 时间复杂度方面,初始化时需要遍历二叉树,时间复杂度为 $O(n)$,查找时只需要判断哈希表中是否存在目标值,时间复杂度为 $O(1)$。空间复杂度 $O(n)$。其中 $n$ 为二叉树节点个数。 @@ -103,9 +103,10 @@ findElements.find(5); // return True # self.left = left # self.right = right class FindElements: + def __init__(self, root: Optional[TreeNode]): - def dfs(root): - self.vis.add(root.val) + def dfs(root: Optional[TreeNode]): + self.s.add(root.val) if root.left: root.left.val = root.val * 2 + 1 dfs(root.left) @@ -114,11 +115,11 @@ class FindElements: dfs(root.right) root.val = 0 - self.vis = set() + self.s = set() dfs(root) def find(self, target: int) -> bool: - return target in self.vis + return target in self.s # Your FindElements object will be instantiated and called as such: @@ -143,15 +144,19 @@ class FindElements: * } */ class FindElements { - private Set vis = new HashSet<>(); + private Set s = new HashSet<>(); public FindElements(TreeNode root) { root.val = 0; dfs(root); } + public boolean find(int target) { + return s.contains(target); + } + private void dfs(TreeNode root) { - vis.add(root.val); + s.add(root.val); if (root.left != null) { root.left.val = root.val * 2 + 1; dfs(root.left); @@ -161,10 +166,6 @@ class FindElements { dfs(root.right); } } - - public boolean find(int target) { - return vis.contains(target); - } } /** @@ -190,26 +191,27 @@ class FindElements { public: FindElements(TreeNode* root) { root->val = 0; - function dfs = [&](TreeNode* root) { - vis.insert(root->val); - if (root->left) { - root->left->val = root->val * 2 + 1; - dfs(root->left); - } - if (root->right) { - root->right->val = root->val * 2 + 2; - dfs(root->right); - } - }; dfs(root); } bool find(int target) { - return vis.count(target); + return s.contains(target); } private: - unordered_set vis; + unordered_set s; + + void dfs(TreeNode* root) { + s.insert(root->val); + if (root->left) { + root->left->val = root->val * 2 + 1; + dfs(root->left); + } + if (root->right) { + root->right->val = root->val * 2 + 2; + dfs(root->right); + } + }; }; /** @@ -229,15 +231,15 @@ private: * } */ type FindElements struct { - vis map[int]bool + s map[int]bool } func Constructor(root *TreeNode) FindElements { root.Val = 0 - vis := map[int]bool{} + s := map[int]bool{} var dfs func(*TreeNode) dfs = func(root *TreeNode) { - vis[root.Val] = true + s[root.Val] = true if root.Left != nil { root.Left.Val = root.Val*2 + 1 dfs(root.Left) @@ -248,11 +250,11 @@ func Constructor(root *TreeNode) FindElements { } } dfs(root) - return FindElements{vis} + return FindElements{s} } func (this *FindElements) Find(target int) bool { - return this.vis[target] + return this.s[target] } /** @@ -262,6 +264,52 @@ func (this *FindElements) Find(target int) bool { */ ``` +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +class FindElements { + private s: Set = new Set(); + + constructor(root: TreeNode | null) { + root.val = 0; + const dfs = (root: TreeNode) => { + this.s.add(root.val); + if (root.left) { + root.left.val = root.val * 2 + 1; + dfs(root.left); + } + if (root.right) { + root.right.val = root.val * 2 + 2; + dfs(root.right); + } + }; + dfs(root); + } + + find(target: number): boolean { + return this.s.has(target); + } +} + +/** + * Your FindElements object will be instantiated and called as such: + * var obj = new FindElements(root) + * var param_1 = obj.find(target) + */ +``` + diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md index be6a9fad33f3a..7c10b7e382d04 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md @@ -82,9 +82,9 @@ findElements.find(5); // return True ### Solution 1: DFS + Hash Table -First, we traverse the binary tree using DFS to restore the node values to their original values. Then, we store all node values in a hash table, so we can directly check whether the target value exists in the hash table when searching. +First, we traverse the binary tree using DFS, restore the node values to their original values, and store all node values in a hash table. Then, when searching, we only need to check if the target value exists in the hash table. -In terms of time complexity, we need to traverse the binary tree during initialization, so the time complexity is $O(n)$. When searching, we only need to check whether the target value exists in the hash table, so the time complexity is $O(1)$. The space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree. +In terms of time complexity, it takes $O(n)$ time to traverse the binary tree during initialization, and $O(1)$ time to check if the target value exists in the hash table during search. The space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree. @@ -96,9 +96,10 @@ In terms of time complexity, we need to traverse the binary tree during initiali # self.left = left # self.right = right class FindElements: + def __init__(self, root: Optional[TreeNode]): - def dfs(root): - self.vis.add(root.val) + def dfs(root: Optional[TreeNode]): + self.s.add(root.val) if root.left: root.left.val = root.val * 2 + 1 dfs(root.left) @@ -107,11 +108,11 @@ class FindElements: dfs(root.right) root.val = 0 - self.vis = set() + self.s = set() dfs(root) def find(self, target: int) -> bool: - return target in self.vis + return target in self.s # Your FindElements object will be instantiated and called as such: @@ -136,15 +137,19 @@ class FindElements: * } */ class FindElements { - private Set vis = new HashSet<>(); + private Set s = new HashSet<>(); public FindElements(TreeNode root) { root.val = 0; dfs(root); } + public boolean find(int target) { + return s.contains(target); + } + private void dfs(TreeNode root) { - vis.add(root.val); + s.add(root.val); if (root.left != null) { root.left.val = root.val * 2 + 1; dfs(root.left); @@ -154,10 +159,6 @@ class FindElements { dfs(root.right); } } - - public boolean find(int target) { - return vis.contains(target); - } } /** @@ -183,26 +184,27 @@ class FindElements { public: FindElements(TreeNode* root) { root->val = 0; - function dfs = [&](TreeNode* root) { - vis.insert(root->val); - if (root->left) { - root->left->val = root->val * 2 + 1; - dfs(root->left); - } - if (root->right) { - root->right->val = root->val * 2 + 2; - dfs(root->right); - } - }; dfs(root); } bool find(int target) { - return vis.count(target); + return s.contains(target); } private: - unordered_set vis; + unordered_set s; + + void dfs(TreeNode* root) { + s.insert(root->val); + if (root->left) { + root->left->val = root->val * 2 + 1; + dfs(root->left); + } + if (root->right) { + root->right->val = root->val * 2 + 2; + dfs(root->right); + } + }; }; /** @@ -222,15 +224,15 @@ private: * } */ type FindElements struct { - vis map[int]bool + s map[int]bool } func Constructor(root *TreeNode) FindElements { root.Val = 0 - vis := map[int]bool{} + s := map[int]bool{} var dfs func(*TreeNode) dfs = func(root *TreeNode) { - vis[root.Val] = true + s[root.Val] = true if root.Left != nil { root.Left.Val = root.Val*2 + 1 dfs(root.Left) @@ -241,11 +243,11 @@ func Constructor(root *TreeNode) FindElements { } } dfs(root) - return FindElements{vis} + return FindElements{s} } func (this *FindElements) Find(target int) bool { - return this.vis[target] + return this.s[target] } /** @@ -255,6 +257,52 @@ func (this *FindElements) Find(target int) bool { */ ``` +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +class FindElements { + private s: Set = new Set(); + + constructor(root: TreeNode | null) { + root.val = 0; + const dfs = (root: TreeNode) => { + this.s.add(root.val); + if (root.left) { + root.left.val = root.val * 2 + 1; + dfs(root.left); + } + if (root.right) { + root.right.val = root.val * 2 + 2; + dfs(root.right); + } + }; + dfs(root); + } + + find(target: number): boolean { + return this.s.has(target); + } +} + +/** + * Your FindElements object will be instantiated and called as such: + * var obj = new FindElements(root) + * var param_1 = obj.find(target) + */ +``` + diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp index 60b6f42d8d4c7..c4f7595369057 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp @@ -13,26 +13,27 @@ class FindElements { public: FindElements(TreeNode* root) { root->val = 0; - function dfs = [&](TreeNode* root) { - vis.insert(root->val); - if (root->left) { - root->left->val = root->val * 2 + 1; - dfs(root->left); - } - if (root->right) { - root->right->val = root->val * 2 + 2; - dfs(root->right); - } - }; dfs(root); } - + bool find(int target) { - return vis.count(target); + return s.contains(target); } private: - unordered_set vis; + unordered_set s; + + void dfs(TreeNode* root) { + s.insert(root->val); + if (root->left) { + root->left->val = root->val * 2 + 1; + dfs(root->left); + } + if (root->right) { + root->right->val = root->val * 2 + 2; + dfs(root->right); + } + }; }; /** diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.go b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.go index 5d9ae9e3c9163..f76b8a0861b51 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.go +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.go @@ -7,15 +7,15 @@ * } */ type FindElements struct { - vis map[int]bool + s map[int]bool } func Constructor(root *TreeNode) FindElements { root.Val = 0 - vis := map[int]bool{} + s := map[int]bool{} var dfs func(*TreeNode) dfs = func(root *TreeNode) { - vis[root.Val] = true + s[root.Val] = true if root.Left != nil { root.Left.Val = root.Val*2 + 1 dfs(root.Left) @@ -26,11 +26,11 @@ func Constructor(root *TreeNode) FindElements { } } dfs(root) - return FindElements{vis} + return FindElements{s} } func (this *FindElements) Find(target int) bool { - return this.vis[target] + return this.s[target] } /** diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.java b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.java index 8cb308f69c0e5..9861770263419 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.java +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.java @@ -14,15 +14,19 @@ * } */ class FindElements { - private Set vis = new HashSet<>(); + private Set s = new HashSet<>(); public FindElements(TreeNode root) { root.val = 0; dfs(root); } + + public boolean find(int target) { + return s.contains(target); + } private void dfs(TreeNode root) { - vis.add(root.val); + s.add(root.val); if (root.left != null) { root.left.val = root.val * 2 + 1; dfs(root.left); @@ -32,10 +36,6 @@ private void dfs(TreeNode root) { dfs(root.right); } } - - public boolean find(int target) { - return vis.contains(target); - } } /** diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.py b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.py index d07e739547d42..f1c2072c0d934 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.py +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.py @@ -5,9 +5,10 @@ # self.left = left # self.right = right class FindElements: + def __init__(self, root: Optional[TreeNode]): - def dfs(root): - self.vis.add(root.val) + def dfs(root: Optional[TreeNode]): + self.s.add(root.val) if root.left: root.left.val = root.val * 2 + 1 dfs(root.left) @@ -16,11 +17,11 @@ def dfs(root): dfs(root.right) root.val = 0 - self.vis = set() + self.s = set() dfs(root) def find(self, target: int) -> bool: - return target in self.vis + return target in self.s # Your FindElements object will be instantiated and called as such: diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.ts b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.ts new file mode 100644 index 0000000000000..66325c19fe0a0 --- /dev/null +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.ts @@ -0,0 +1,43 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +class FindElements { + private s: Set = new Set(); + + constructor(root: TreeNode | null) { + root.val = 0; + const dfs = (root: TreeNode) => { + this.s.add(root.val); + if (root.left) { + root.left.val = root.val * 2 + 1; + dfs(root.left); + } + if (root.right) { + root.right.val = root.val * 2 + 2; + dfs(root.right); + } + }; + dfs(root); + } + + find(target: number): boolean { + return this.s.has(target); + } +} + +/** + * Your FindElements object will be instantiated and called as such: + * var obj = new FindElements(root) + * var param_1 = obj.find(target) + */