question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
largest-submatrix-with-rearrangements | simple sorting prefix sum problem | simple-sorting-prefix-sum-problem-by-ani-urih | \nclass Solution {\n public int largestSubmatrix(int[][] matrix) {\n int n = matrix.length;\n int m = matrix[0].length;\n for(int i=0;i< | anilkumawat3104 | NORMAL | 2023-11-26T08:44:16.272697+00:00 | 2023-11-26T08:44:16.272731+00:00 | 76 | false | ```\nclass Solution {\n public int largestSubmatrix(int[][] matrix) {\n int n = matrix.length;\n int m = matrix[0].length;\n for(int i=0;i<m;i++){\n for(int j=1;j<n;j++){\n if(matrix[j][i]==0)\n continue;\n else{\n ma... | 1 | 0 | ['Greedy', 'Sorting', 'Prefix Sum', 'Java'] | 0 |
largest-submatrix-with-rearrangements | Pyhton | Easy | Greedy | pyhton-easy-greedy-by-khosiyat-n4my | see the Successfully Accepted Submission\npython\nclass Solution:\n def largestSubmatrix(self, matrix):\n rows, cols = len(matrix), len(matrix[0])\n | Khosiyat | NORMAL | 2023-11-26T08:09:31.605704+00:00 | 2023-11-26T08:09:31.605727+00:00 | 23 | false | [see the Successfully Accepted Submission](https://leetcode.com/submissions/detail/1106639757/)\n```python\nclass Solution:\n def largestSubmatrix(self, matrix):\n rows, cols = len(matrix), len(matrix[0])\n max_area = 0\n\n for j in range(cols):\n cont_sum = 0\n for i in ra... | 1 | 0 | ['Greedy', 'Python'] | 0 |
largest-submatrix-with-rearrangements | [We💕Simple] Counting Sort, Dynamic Programming w/ Explanations | wesimple-counting-sort-dynamic-programmi-k6dv | I always like to find solutions that are very simple and easy, yet do not sacrifice time complexity. I keep creating more solutions\n\n# Code\nKotlin []\nclass | YooSeungKim | NORMAL | 2023-11-26T07:04:59.101778+00:00 | 2023-11-26T13:31:12.686186+00:00 | 167 | false | I always like to find solutions that are very simple and easy, yet do not sacrifice time complexity. I keep creating more solutions\n\n# Code\n``` Kotlin []\nclass Solution {\n fun largestSubmatrix(matrix: Array<IntArray>): Int {\n val heights = IntArray(matrix[0].size)\n var maxArea = 0\n for (... | 1 | 0 | ['Dynamic Programming', 'Counting Sort', 'Python', 'Python3', 'Kotlin'] | 0 |
largest-submatrix-with-rearrangements | Simple solution with explaination | simple-solution-with-explaination-by-nip-3fxw | Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n\n\n\n# Code\n\nclass Solution {\n public int largestSubmatrix(int[][] matrix) { | Nipun0803 | NORMAL | 2023-11-26T06:56:34.223832+00:00 | 2023-11-26T06:56:34.223859+00:00 | 16 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n\n\n# Code\n```\nclass Solution {\n public int largestSubmatrix(int[][] matrix) {\n int m = matrix.length, n... | 1 | 0 | ['Java'] | 0 |
largest-submatrix-with-rearrangements | Easy || Prerequisites || Happy Coding | easy-prerequisites-happy-coding-by-shrey-q7ed | There are two prerequisites for this problem: the maximal square and the maximal rectangle. Once you understand these concepts, this question becomes relatively | shreyas_6379 | NORMAL | 2023-11-26T06:33:14.623003+00:00 | 2023-11-26T06:33:14.623045+00:00 | 65 | false | There are two prerequisites for this problem: the maximal square and the maximal rectangle. Once you understand these concepts, this question becomes relatively straightforward to grasp and solve.\n\n# Code\n```\n\n\nclass Solution {\npublic:\n int largestSubmatrix(vector<vector<int>>& matrix) {\n int m = mat... | 1 | 0 | ['C++'] | 0 |
largest-submatrix-with-rearrangements | Easy explanation with example | Space complexity O(1) | beats 90% C# users | easy-explanation-with-example-space-comp-femr | Intuition\nSubmatrix would either be a squre matrix or rectangular matrix, so we need to traverse each column and check the number of consicutive ones and then | rahulgulia | NORMAL | 2023-11-26T06:30:22.175175+00:00 | 2023-11-26T06:30:22.175198+00:00 | 48 | false | # Intuition\nSubmatrix would either be a squre matrix or rectangular matrix, so we need to traverse each column and check the number of consicutive ones and then arrange row elements to get the biggest submatrix possible.\n\n# Approach\n### Step 1\nTraverse each column and modify row elements in such a way that if \'i\... | 1 | 0 | ['C#'] | 0 |
largest-submatrix-with-rearrangements | JAVA Code Solution Explained in HINDI | java-code-solution-explained-in-hindi-by-j2f0 | https://youtu.be/7jTSxIzUEEk\n\nPlease, check above video for explanation and do like and subscribe the channel.\n\n# Code\n\nclass Solution {\n public int l | The_elite | NORMAL | 2023-11-26T06:20:26.288036+00:00 | 2023-11-26T06:20:26.288068+00:00 | 1 | false | https://youtu.be/7jTSxIzUEEk\n\nPlease, check above video for explanation and do like and subscribe the channel.\n\n# Code\n```\nclass Solution {\n public int largestSubmatrix(int[][] matrix) {\n\n int m = matrix.length;\n int n = matrix[0].length;\n\n int ans = 0;\n\n for(int r = 0; r < m; r++) ... | 1 | 0 | ['Java'] | 0 |
largest-submatrix-with-rearrangements | ruby 2-liner | ruby-2-liner-by-_-k-qzot | \ndef largest_submatrix(m)\n\n a = m.transpose.map{|r| r.chunk_while{ 1<_1+_2 }.map &:sum }\n m.map{ a.sort.reverse.zip(1..).map{|x,i| i*x[0].tap{ x.shift if | _-k- | NORMAL | 2023-11-26T05:50:17.208564+00:00 | 2023-11-26T05:50:17.208584+00:00 | 12 | false | ```\ndef largest_submatrix(m)\n\n a = m.transpose.map{|r| r.chunk_while{ 1<_1+_2 }.map &:sum }\n m.map{ a.sort.reverse.zip(1..).map{|x,i| i*x[0].tap{ x.shift if 1>x[0]-=1 } }.max }.max\n\nend\n``` | 1 | 0 | ['Ruby'] | 0 |
largest-submatrix-with-rearrangements | Kotlin | kotlin-by-samoylenkodmitry-s5u4 | \n\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/417\n\n#### Problem TLDR\n\nMax area of 1 submatrix after sorting columns optimally\n\n# | SamoylenkoDmitry | NORMAL | 2023-11-26T05:46:58.791856+00:00 | 2023-11-26T05:46:58.791880+00:00 | 27 | false | \n\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/417\n\n#### Problem TLDR\n\nMax area of `1` submatrix after sorting columns optimally\n\n#### Intuition\n\nUse hint :(\nOk, if we stor... | 1 | 0 | ['Sorting', 'Kotlin'] | 0 |
largest-submatrix-with-rearrangements | 5 Lines Solution with good explanation | 5-lines-solution-with-good-explanation-b-b4ws | This C++ solution is designed to find the size of the largest submatrix containing only 1s in a given matrix a. Here\'s a step-by-step explanation:\n\n1. Initia | Chouhan_Gourav | NORMAL | 2023-11-26T05:37:32.603133+00:00 | 2023-11-26T05:37:32.603152+00:00 | 261 | false | This C++ solution is designed to find the size of the largest submatrix containing only 1s in a given matrix `a`. Here\'s a step-by-step explanation:\n\n1. **Initialize Variables:**\n - `n` and `m` store the number of rows and columns in the matrix, respectively.\n - `ans` is the variable to keep track of the maxim... | 1 | 0 | ['C++'] | 1 |
largest-submatrix-with-rearrangements | Easy Way || Sorting || Prefix Array || CPP || Matrix | easy-way-sorting-prefix-array-cpp-matrix-2cx9 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | shubham7447 | NORMAL | 2023-11-26T05:35:58.560699+00:00 | 2023-11-26T05:36:43.344704+00:00 | 72 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['Array', 'Sorting', 'Matrix', 'C++'] | 0 |
largest-submatrix-with-rearrangements | [Golang] No Sorting | golang-no-sorting-by-vasakris-ms1n | Complexity\n- Time complexity:\nO(m*n)\n\n- Space complexity:\nO(n)\n\n# Code\n\ntype Pair struct {\n Height int\n Column int\n}\n\nfunc largestSubmatrix( | vasakris | NORMAL | 2023-11-26T05:29:57.277417+00:00 | 2023-11-26T05:29:57.277444+00:00 | 61 | false | # Complexity\n- Time complexity:\nO(m*n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\ntype Pair struct {\n Height int\n Column int\n}\n\nfunc largestSubmatrix(matrix [][]int) int {\n m, n := len(matrix), len(matrix[0])\n prevHeights := []Pair{}\n res := 0\n\n for row := 0; row < m; row++ {\n he... | 1 | 0 | ['Go'] | 0 |
largest-submatrix-with-rearrangements | Histogram-like Approach | histogram-like-approach-by-hsakib8685-mgif | Intuition\nThe challenge was to find the largest 1\'s submatrix after rearranging columns. My initial thought was to transform the problem into a histogram-like | hsakib8685 | NORMAL | 2023-11-26T05:16:01.455233+00:00 | 2023-11-26T05:16:01.455265+00:00 | 6 | false | # Intuition\nThe challenge was to find the largest `1`\'s submatrix after rearranging **columns**. My initial thought was to transform the problem into a **histogram**-like approach, treating rows as bases and columns as heights.\n\n# Approach\n1. **Height Calculation**: First, transform the matrix such that each ce... | 1 | 0 | ['Array', 'Greedy', 'Sorting', 'Matrix', 'C++'] | 0 |
largest-submatrix-with-rearrangements | 90ms C++ beats 100% O(mn) | 90ms-c-beats-100-omn-by-yjian012-pw7l | Before the O(mn) solution, first the O(m n log n) solution (but slightly improved), best time 102ms.\n\n//https://hyper-meta.blogspot.com/\n#pragma GCC target(" | yjian012 | NORMAL | 2023-11-26T05:12:28.091036+00:00 | 2023-11-26T06:07:29.428297+00:00 | 18 | false | Before the O(mn) solution, first the O(m n log n) solution (but slightly improved), best time 102ms.\n```\n//https://hyper-meta.blogspot.com/\n#pragma GCC target("avx,mmx,sse2,sse3,sse4")\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\nint t[100001];\nint ind[100001];\nint isz;\nclass... | 1 | 0 | ['C++'] | 0 |
largest-submatrix-with-rearrangements | 🥷Beats 100% | Simple dynamic programming approach | No extra space 🔥 | beats-100-simple-dynamic-programming-app-rf1w | Intuition\n\nWhen approaching the problem of finding the largest submatrix of 1s in a matrix, the initial thought involves exploring a method to identify a cont | jailwalankit | NORMAL | 2023-11-26T04:31:45.802250+00:00 | 2023-11-26T04:31:45.802272+00:00 | 84 | false | # Intuition\n\nWhen approaching the problem of finding the largest submatrix of 1s in a matrix, the initial thought involves exploring a method to identify a contiguous area that comprises only 1s. It\'s crucial to establish a systematic way to track the maximum possible area of such a submatrix within the given matrix... | 1 | 0 | ['Array', 'Sorting', 'Matrix', 'C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | Python Special Way for find() without HashSet O(1) Space O(logn) Time | python-special-way-for-find-without-hash-8dzg | It\'s obvious to use BFS for the initial part. However, a lot of people use HashSet(set() in python) to pre-store all the values in the initial part, which may | zhaoqxu | NORMAL | 2019-11-17T05:32:30.393615+00:00 | 2019-11-17T05:32:30.393653+00:00 | 8,129 | false | It\'s obvious to use `BFS` for the initial part. However, a lot of people use HashSet(`set()` in python) to pre-store all the values in the initial part, which may cause MLE when the values are huge. There is a special way to implement `find()` that costs O(1) in space and O(logn) in time. \n\nFirstly, let\'s see what... | 164 | 1 | ['Python3'] | 22 |
find-elements-in-a-contaminated-binary-tree | [Java/Python 3] DFS, BFS and Bit Manipulation clean codes w/ analysis. | javapython-3-dfs-and-bfs-clean-codes-w-a-9776 | DFSBFS- inspired by@MichaelZ.Analysis:HashSet cost spaceO(N),dfs()cost spaceO(H)and timeO(N),bfs()cost time and spaceO(N), thereforeFindElements() (dfs() and bf | rock | NORMAL | 2019-11-17T04:10:57.578568+00:00 | 2025-02-21T18:53:39.634616+00:00 | 9,556 | false | **DFS**
```java
private Set<Integer> seen = new HashSet<>();
public FindElements(TreeNode root) {
dfs(root, 0);
}
private void dfs(TreeNode n, int v) {
if (n == null) return;
seen.add(v);
n.val = v;
dfs(n.left, 2 * v + 1);
dfs(n.right, 2 * v + 2);
... | 80 | 4 | ['Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'Java', 'Python3'] | 16 |
find-elements-in-a-contaminated-binary-tree | Beats 100% | Efficient Binary Tree Recovery | Bit Manipulation & Set Storage 🌳 | beats-100-efficient-binary-tree-recovery-hshk | Youtube🚀Beats 100% | Efficient Binary Tree Recovery | Bit Manipulation & Set Storage 🌳🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote💡If this helpe | rahulvijayan2291 | NORMAL | 2025-02-21T05:11:46.338463+00:00 | 2025-02-21T05:11:46.338463+00:00 | 13,165 | false | # Youtube
https://youtu.be/vmQHt247oxQ

---
# 🚀 **Beats 100% | Efficient Binary Tree Recovery | Bit Manipulation & Set Storage 🌳**
---
# **🔼 Please Upvote**
# **🔼 Please Upvote**
# **🔼 Please ... | 70 | 0 | ['Bit Manipulation', 'Tree', 'Binary Search Tree', 'Binary Tree', 'C++', 'Java', 'Python3', 'JavaScript'] | 4 |
find-elements-in-a-contaminated-binary-tree | Java bit path Time : O(logn) | java-bit-path-time-ologn-by-jiaweizhang1-xrlf | Think about it with complete binary tree and the whole idea is to binary serialize the target number. It\'s actually tha order we doing the dfs.\nLet\'s say our | jiaweizhang1994 | NORMAL | 2019-11-17T08:21:54.813591+00:00 | 2019-11-18T02:50:24.048398+00:00 | 3,428 | false | Think about it with complete binary tree and the whole idea is to binary serialize the target number. It\'s actually tha order we doing the dfs.\nLet\'s say our target : 9 we shall take it as 10 : 1010 ignoring the first digit 1 the path ( 010 ) means left -> right -> left \nSo \'0\' means going to the left and \'1\... | 50 | 2 | [] | 10 |
find-elements-in-a-contaminated-binary-tree | C++ simple and easy to understand | c-simple-and-easy-to-understand-by-dsmn-a1b2 | \nclass FindElements {\n unordered_set<int> set;\npublic:\n void recover(TreeNode* root, int x) {\n if (!root) return;\n root->val = x;\n | dsmn | NORMAL | 2019-11-21T03:01:29.063470+00:00 | 2019-11-21T03:01:29.063503+00:00 | 5,239 | false | ```\nclass FindElements {\n unordered_set<int> set;\npublic:\n void recover(TreeNode* root, int x) {\n if (!root) return;\n root->val = x;\n set.emplace(x);\n recover(root->left, 2 * x + 1);\n recover(root->right, 2 * x + 2);\n }\n \n FindElements(TreeNode* root) {\n ... | 42 | 1 | ['Recursion', 'C', 'Ordered Set'] | 5 |
find-elements-in-a-contaminated-binary-tree | 🌲 [🔥 Recover Contaminated Binary Tree | Fast & Clean C++/Python Solution] 💡 | recover-contaminated-binary-tree-fast-cl-cckf | IntroductionHey LeetCoders! 👋Today, we're tackling aninteresting tree problem—recovering a contaminated binary tree! The challenge here is to reconstruct a tree | lasheenwael9 | NORMAL | 2025-02-21T00:38:07.426537+00:00 | 2025-02-21T05:47:51.666414+00:00 | 4,402 | false | # Introduction
Hey LeetCoders! 👋
Today, we're tackling an **`interesting tree problem`**—recovering a contaminated binary tree! The challenge here is to reconstruct a tree that follows a **`specific mathematical pattern`** and then efficiently check if a given target exists.
We'll be using **`DFS (Depth-First Sea... | 33 | 1 | ['Hash Table', 'Tree', 'Depth-First Search', 'Design', 'Binary Tree', 'Ordered Set', 'C++', 'Python3'] | 8 |
find-elements-in-a-contaminated-binary-tree | Beat 100% | Most optimal | Bit manipulation + binary search in Python and C# | O(log(n)) time | beat-100-most-optimal-bit-manipulation-b-trq3 | IntuitionThe key observation is that we do not actually need to “recover” the whole tree explicitly. Given the recovery rules—assigning the root a value of 0 an | Zyr0nX | NORMAL | 2025-02-19T04:43:07.077682+00:00 | 2025-02-21T03:17:46.268171+00:00 | 2,451 | false | # Intuition
The key observation is that we do not actually need to “recover” the whole tree explicitly. Given the recovery rules—assigning the root a value of 0 and for any node with value x, its left child is 2\*x + 1 and its right child is 2\*x + 2—we can deduce that if we add 1 to any target value, its binary repres... | 25 | 1 | ['Binary Search', 'Bit Manipulation', 'Binary Search Tree', 'Binary Tree', 'Bitmask', 'Python3', 'C#'] | 4 |
find-elements-in-a-contaminated-binary-tree | DFS+bitset vs BFS||C++ Py3 beats 100% | dfsbitsetc-beats-100-by-anwendeng-nzxk | IntuitionDFS+ bitsetUse DFS to traverse the binary tree with bitset as a container for marking the value of node is seen instead of a hash table.BFS is also mad | anwendeng | NORMAL | 2025-02-21T00:24:28.947657+00:00 | 2025-02-21T06:43:33.798438+00:00 | 3,466 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
DFS+ bitset
Use DFS to traverse the binary tree with bitset as a container for marking the value of node is seen instead of a hash table.
BFS is also made for C++ & Python
# Approach
<!-- Describe your approach to solving the problem. -->
1... | 22 | 0 | ['Depth-First Search', 'Breadth-First Search', 'C++', 'Python3'] | 10 |
find-elements-in-a-contaminated-binary-tree | Beats 98.92% | DFS | Solution for LeetCode#1261 | dfs-solution-for-leetcode1261-by-samir02-7z7p | IntuitionThe problem involves a binary tree that has been "contaminated" by changing all its values to -1. The goal is to "recover" the tree and implement a fin | samir023041 | NORMAL | 2025-02-21T01:24:44.394155+00:00 | 2025-02-21T05:59:18.983399+00:00 | 4,582 | false | 
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves a binary tree that has been "contaminated" by changing all its values to -1. The goal is to "recover" ... | 17 | 0 | ['Depth-First Search', 'Python', 'Java', 'Python3'] | 5 |
find-elements-in-a-contaminated-binary-tree | ✅ Beats 100% 🔥||🧑💻 BEGINNER FREINDLY||🌟JAVA||C++ | beats-100-beginner-freindlyjavac-by-asho-agx9 | IntuitionApproach1.Recover a contaminated binary tree (all nodes = -1) to its original state:. Root value = 0.
. Left child of x = 2 * x + 1.
. Right child of x | Varma5247 | NORMAL | 2025-02-21T01:17:00.153201+00:00 | 2025-02-25T06:24:51.918724+00:00 | 1,804 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
1.Recover a contaminated binary tree (all nodes = ```-1```) to its original state:
. Root value = ```0```.
. Left child of ```x``` = ```2 * x + 1```.
. Right child of ```x... | 16 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'C++', 'Java'] | 1 |
find-elements-in-a-contaminated-binary-tree | ✅💯 % ON RUNTIME | 😁EASY SOLUTION | 😎PROPER EXPLANATION | on-runtime-easy-solution-proper-explanat-5uf1 | Here’s a well-structured and detailed version of your solution explanation:🧠 IntuitionWhen given a contaminated binary tree where every node's value is set to-1 | IamHazra | NORMAL | 2025-02-21T00:32:40.204822+00:00 | 2025-02-21T01:00:36.433467+00:00 | 1,942 | false | Here’s a well-structured and detailed version of your solution explanation:
---
# 🧠 Intuition
When given a contaminated binary tree where every node's value is set to `-1`, our goal is to recover it based on certain rules:
- The root node's value is set to `0`.
- The left child of a node with value `x` has a... | 16 | 0 | ['Hash Table', 'Tree', 'Recursion', 'Python', 'Java', 'Python3', 'JavaScript', 'C#'] | 3 |
find-elements-in-a-contaminated-binary-tree | 🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏| JAVA | C | C++ | PYTHON| JAVASCRIPT | DART | beats-super-easy-beginners-java-c-c-pyth-ww99 | 🚀 IntuitionThe problem gives us acontaminated binary tree, where each node's value is corrupted.We need torecover the treeby following these rules:
The root is | CodeWithSparsh | NORMAL | 2025-02-21T06:43:26.485534+00:00 | 2025-02-21T06:43:26.485534+00:00 | 583 | false | 
---
## **🚀 Intuition**
The problem gives us a **contaminated binary tree**, where each node's value is corrupted.
We need to **recover the tree** by following these rules:
1. The root is set to ... | 10 | 0 | ['Tree', 'Depth-First Search', 'C', 'Binary Tree', 'C++', 'Java', 'Python3', 'JavaScript', 'Dart', 'Python ML'] | 1 |
find-elements-in-a-contaminated-binary-tree | Intuitive C++ code beats 100% in memory | intuitive-c-code-beats-100-in-memory-by-7e8kq | \nclass FindElements {\n TreeNode* head=new TreeNode(-1);\npublic:\n FindElements(TreeNode* root) {\n head=root;\n head->val=0;\n rec | pi14 | NORMAL | 2019-11-17T12:36:59.024294+00:00 | 2019-11-17T12:36:59.024331+00:00 | 1,785 | false | ```\nclass FindElements {\n TreeNode* head=new TreeNode(-1);\npublic:\n FindElements(TreeNode* root) {\n head=root;\n head->val=0;\n recover(head);\n }\n \n void recover(TreeNode* root)\n {\n if(root->left)\n {\n root->left->val=(root->val)*2+1;\n ... | 10 | 2 | ['Recursion', 'C'] | 3 |
find-elements-in-a-contaminated-binary-tree | C++ || Simple Solution || Tc=O(log(N)) || SC=O(1) | c-simple-solution-tcologn-sco1-by-abhish-h4f4 | \n\n# Complexity\n- Time complexity:\nO(log(N))\n\n- Space complexity:\nO(log(N))\n# Code\n\nclass FindElements\n{\npublic:\n TreeNode *root;\n FindElemen | Abhishek26149 | NORMAL | 2023-02-11T13:38:45.902583+00:00 | 2023-02-11T13:38:45.902609+00:00 | 934 | false | \n\n# Complexity\n- Time complexity:\nO(log(N))\n\n- Space complexity:\nO(log(N))\n# Code\n```\nclass FindElements\n{\npublic:\n TreeNode *root;\n FindElements(TreeNode *root2)\n {\n root = root2;\n }\n\n bool find(int tar)\n {\n stack<int> st;\n while (tar > 0)\n {\n ... | 9 | 0 | ['C++'] | 1 |
find-elements-in-a-contaminated-binary-tree | Java bit representation of (target + 1) solution with explanation | java-bit-representation-of-target-1-solu-8daf | if the root.val is 1 instead of 0, then the values of tree node are:\n\n\t\t\t\t\t\t\t\t\t1\n\t\t\t\t\t 2 3\n\t\t 4 5 | weibiny | NORMAL | 2019-12-31T05:00:52.413063+00:00 | 2019-12-31T05:19:02.593526+00:00 | 460 | false | if the root.val is 1 instead of 0, then the values of tree node are:\n```\n\t\t\t\t\t\t\t\t\t1\n\t\t\t\t\t 2 3\n\t\t 4 5 6 7\n 8 \t\t9 10 11 12 13 14 15\n .... ..... .... .... ...... | 9 | 0 | [] | 2 |
find-elements-in-a-contaminated-binary-tree | Python binary path without set | python-binary-path-without-set-by-flag20-ftr8 | bin(9 + 1) = 1 010\nStart from the second digit: 0/1/0 means left/right/left when traverse if 9 exists.\nrecover: T: O(n), S: O(n) (DFS worst case)\nfind: T: O( | flag2019 | NORMAL | 2019-11-17T06:32:27.689551+00:00 | 2019-11-18T04:11:39.192952+00:00 | 1,487 | false | bin(9 + 1) = 1 010\nStart from the second digit: 0/1/0 means left/right/left when traverse if 9 exists.\nrecover: T: O(n), S: O(n) (DFS worst case)\nfind: T: O(logn), S: O(logn) (for the string of path encoding, can be O(1) if use bit mask) \n```\nclass FindElements(object):\n\n def __init__(self, root):\n ""... | 9 | 1 | [] | 1 |
find-elements-in-a-contaminated-binary-tree | Simple DFS Approach✅✅ | simple-dfs-approach-by-arunk_leetcode-o4ww | Intuition:
The given binary tree is contaminated, meaning the values are incorrect.
The goal is to reconstruct the tree by following a rule where:
The root node | arunk_leetcode | NORMAL | 2025-02-21T06:19:09.076002+00:00 | 2025-02-21T06:20:36.631437+00:00 | 647 | false |
# Intuition:
* The given binary tree is contaminated, meaning the values are incorrect.
* The goal is to reconstruct the tree by following a rule where:
- The root node is assigned the `value 0`.
- If a node has value x, then its left child is assigned `(2 * x + 1)` and right child is `(2 * x + 2)`.
* Usin... | 8 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3'] | 2 |
find-elements-in-a-contaminated-binary-tree | NEW Approach! Branchless. Time O(1) OR space O(1). Beats 100.00% | show-me-da-wae-branchless-ologn-o1-beats-78qq | IntuitionDepth-first tree traversal, calculating the values of child nodes using formula2 * x + 1and2 * x + 2forleftandright. Use aSetorArrayto store node val | nodeforce | NORMAL | 2025-02-21T12:16:44.042964+00:00 | 2025-02-22T17:21:50.666482+00:00 | 251 | false | # Intuition
Depth-first tree traversal, calculating the values of child nodes using formula `2 * x + 1` and `2 * x + 2` for `left` and `right`. Use a `Set` or `Array` to store node values.
To fit into `O(1)` space, you can honestly search for a path along the tree. To do this, use the formulas above and restore the ... | 7 | 0 | ['Tree', 'Depth-First Search', 'Design', 'C', 'Python', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript'] | 1 |
find-elements-in-a-contaminated-binary-tree | Super Simple Approch✅ 🚀 | Step-By-Step Explained 🔥 | C++ | Java | Python | Js | super-simple-approch-step-by-step-explai-3yac | 🔍 IntuitionThe given binary tree is contaminated, meaning its values are not reliable. However, we know the rule for reconstructing it:
🌱 The root is always0.
🌿 | himanshu_dhage | NORMAL | 2025-02-21T03:31:35.420165+00:00 | 2025-02-21T03:31:35.420165+00:00 | 427 | false | # 🔍 Intuition
The given binary tree is contaminated, meaning its values are not reliable. However, we know the rule for reconstructing it:
- 🌱 The root is always `0`.
- 🌿 The left child of a node with value `x` is `2 * x + 1`.
- 🌳 The right child of a node with value `x` is `2 * x + 2`.
Using this rule, ... | 6 | 0 | ['Tree', 'Depth-First Search', 'Breadth-First Search', 'Design', 'Binary Tree', 'Interactive', 'C++', 'Java', 'Python3', 'JavaScript'] | 0 |
find-elements-in-a-contaminated-binary-tree | Most Optimal Solution ✅BFS | C++| Java | Python | JavaScript | most-optimal-solution-bfs-c-java-python-axq2x | ⬆️Upvote if it helps ⬆️Connect with me on Linkedin [Bijoy Sing]IntuitionThe given binary tree is "contaminated" with all values set to-1. However, we know the o | BijoySingh7 | NORMAL | 2025-02-21T02:46:34.028255+00:00 | 2025-02-21T02:46:34.028255+00:00 | 770 | false | # ⬆️Upvote if it helps ⬆️
---
## Connect with me on Linkedin [Bijoy Sing]
---
```cpp []
class Solution {
public:
unordered_set<int> values;
Solution(TreeNode* root) {
if (!root) return;
queue<pair<TreeNode*, int>> q;
q.push({root, 0});
while (!q.empty()) {
auto ... | 6 | 0 | ['Tree', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 0 |
find-elements-in-a-contaminated-binary-tree | Easy Python | O(1) Find | Recursion + Queue | easy-python-o1-find-recursion-queue-by-a-ldl1 | Easy Python | O(1) Find | Recursion + Queue\n\nA) Standard Code\n\nIn this problem, we can recover the values of the original Binary Tree, and store them in a " | aragorn_ | NORMAL | 2020-08-17T22:41:11.771359+00:00 | 2020-08-17T23:09:22.747850+00:00 | 900 | false | **Easy Python | O(1) Find | Recursion + Queue**\n\n**A) Standard Code**\n\nIn this problem, we can recover the values of the original Binary Tree, and store them in a "set" directly. (There\'s no need to keep the Binary Tree itself)\n\nThis way, we can "find" any element with O(1) time/space complexity. The initializat... | 6 | 1 | ['Python', 'Python3'] | 1 |
find-elements-in-a-contaminated-binary-tree | simple JAVA DSF solution use BitSet,double 100% | simple-java-dsf-solution-use-bitsetdoubl-srz0 | \n class FindElements {\n\n private BitSet sets = new BitSet();\n\n public FindElements(TreeNode root) {\n dsf(root, 0);\n }\ | wangxiongfei | NORMAL | 2019-12-12T10:11:42.384295+00:00 | 2019-12-12T10:14:38.256193+00:00 | 888 | false | ```\n class FindElements {\n\n private BitSet sets = new BitSet();\n\n public FindElements(TreeNode root) {\n dsf(root, 0);\n }\n\n public boolean find(int target) {\n return sets.get(target);\n }\n\n private void dsf(TreeNode root, int val) {\n ... | 6 | 0 | [] | 0 |
find-elements-in-a-contaminated-binary-tree | JAVA Solution Using HashSet. | java-solution-using-hashset-by-yadavrkg4-kgml | IntuitionThe problem involves reconstructing a contaminated binary tree where each node originally followed the rule:The root node starts with a value of 0.The | YadavRKG45 | NORMAL | 2025-02-21T11:03:03.603458+00:00 | 2025-02-21T11:03:03.603458+00:00 | 131 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves reconstructing a contaminated binary tree where each node originally followed the rule:
The root node starts with a value of 0.
The left child of a node with value x has a value of 2 * x + 1.
The right child of a node ... | 5 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Binary Tree', 'Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Simple CPP solution by TGK | simple-cpp-solution-by-tgk-by-kishore_m_-5ene | IntuitionThe given problem is about recovering a binary tree that was corrupted and finding elements in it efficiently.ApproachConstructor (FindElements(TreeNod | KISHORE_M_13 | NORMAL | 2025-02-21T04:00:27.509099+00:00 | 2025-02-21T04:00:27.509099+00:00 | 456 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The given problem is about recovering a binary tree that was corrupted and finding elements in it efficiently.
The tree follows a specific rule where if a node has value x, then:
Its left child has value 2*x + 1
Its rig... | 5 | 0 | ['C++'] | 1 |
find-elements-in-a-contaminated-binary-tree | ✅ EASY JAVA SOLUTION || Easy peasy lemon squeezy😊 || SIMPLE | easy-java-solution-easy-peasy-lemon-sque-8lml | \n# Code\n\nclass FindElements {\n TreeNode root;\n Set<Integer> set = new HashSet<>();\n public FindElements(TreeNode root) {\n this.root = roo | Sauravmehta | NORMAL | 2023-02-18T17:11:40.959767+00:00 | 2023-02-18T17:13:17.600545+00:00 | 721 | false | \n# Code\n```\nclass FindElements {\n TreeNode root;\n Set<Integer> set = new HashSet<>();\n public FindElements(TreeNode root) {\n this.root = root;\n if(root != null){ root.val = 0; set.add(root.val); }\n recover(root,set);\n }\n \n public boolean find(int target) {\n retur... | 5 | 0 | ['Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | C++ | EASY SOLUTION | c-easy-solution-by-priyanshichauhan1998x-ms8l | ```\nclass FindElements {\npublic:\n unordered_set set;\n void recover(TreeNode root , int x){\n if(!root ){\n return ;\n }\n | priyanshichauhan1998x | NORMAL | 2021-10-10T14:32:03.064193+00:00 | 2021-10-10T14:32:03.064231+00:00 | 131 | false | ```\nclass FindElements {\npublic:\n unordered_set<int> set;\n void recover(TreeNode* root , int x){\n if(!root ){\n return ;\n }\n set.insert(x);\n recover(root->left , 2*x+1);\n recover(root->right , 2*x+2);\n \n }\n FindElements(TreeNode* root) {\n ... | 5 | 0 | ['Recursion', 'C'] | 0 |
find-elements-in-a-contaminated-binary-tree | Java simple DFS and Set | java-simple-dfs-and-set-by-hobiter-sifi | \nclass FindElements {\n Set<Integer> st = new HashSet<>();\n public FindElements(TreeNode root) {\n if (root == null) return;\n dfs(root, 0 | hobiter | NORMAL | 2020-06-27T23:37:31.401346+00:00 | 2020-06-27T23:37:31.401373+00:00 | 184 | false | ```\nclass FindElements {\n Set<Integer> st = new HashSet<>();\n public FindElements(TreeNode root) {\n if (root == null) return;\n dfs(root, 0);\n }\n \n private void dfs(TreeNode node, int val) {\n st.add(val);\n if (node.left != null) dfs(node.left, val * 2 + 1);\n i... | 5 | 2 | [] | 0 |
find-elements-in-a-contaminated-binary-tree | [Java] 18ms 92% faster | java-18ms-92-faster-by-aksharkashyap-lua2 | The speed is obtained by not modifiying the tree and using bitwise operator\nleftshift by 1 == multiplication by 2 and it is much faster than multiplication ope | aksharkashyap | NORMAL | 2020-05-22T14:28:53.461169+00:00 | 2021-09-02T05:18:04.906910+00:00 | 636 | false | The speed is obtained by not modifiying the tree and using bitwise operator\nleftshift by 1 == multiplication by 2 and it is much faster than multiplication operator\n\nFor the fast lookup use hashmap, because the lookup time is O(1) (avg)\n```\nclass FindElements {\n\n Set<Integer> set = new HashSet<>();\n void ... | 5 | 0 | ['Java'] | 1 |
find-elements-in-a-contaminated-binary-tree | Python hacks | python-hacks-by-stefanpochmann-bxrr | \nclass FindElements(object):\n def __init__(self, root):\n def v(r, x):\n return r and {x} | v(r.left, 2*x+1) | v(r.right, 2*x+2) or set() | stefanpochmann | NORMAL | 2019-11-26T20:01:09.943522+00:00 | 2019-11-26T21:32:43.325789+00:00 | 832 | false | ```\nclass FindElements(object):\n def __init__(self, root):\n def v(r, x):\n return r and {x} | v(r.left, 2*x+1) | v(r.right, 2*x+2) or set()\n self.find = v(root, 0).__contains__\n```\n\nAnother, based on [this one](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/... | 5 | 0 | [] | 1 |
find-elements-in-a-contaminated-binary-tree | Simple solution | DFS | simple-solution-dfs-by-saurabhdamle11-55wb | IntuitionWe will iterate over the Tree using a DFS and update the values if we ecounter a -1 and store them in a map to check for occurances.Approach
Use DFS to | saurabhdamle11 | NORMAL | 2025-02-21T05:02:39.906492+00:00 | 2025-02-21T05:03:18.904201+00:00 | 205 | false | # Intuition
We will iterate over the Tree using a DFS and update the values if we ecounter a -1 and store them in a map to check for occurances.
# Approach
1. Use DFS to iterate over the tree.
2. If a node has -1 as the value, update it to the expected value.
3. If the children are present, iterate over them with their... | 4 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Design', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3'] | 2 |
find-elements-in-a-contaminated-binary-tree | Find Elements in a Contaminated Binary Tree || Efficient | find-elements-in-a-contaminated-binary-t-j2r5 | IntuitionThe problem involves finding a value in a binary tree where all the nodes' values are initially set to be "corrupted" (e.g., some undefined or arbitrar | Mirin_Mano_M | NORMAL | 2025-02-21T04:01:16.718476+00:00 | 2025-02-21T04:01:16.718476+00:00 | 211 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves finding a value in a binary tree where all the nodes' values are initially set to be "corrupted" (e.g., some undefined or arbitrary values). We need to recover the values by using a specific rule based on their position... | 4 | 0 | ['C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | 🔥 Beats 98.92% 🔥 | 🌟 Proof 🌟 | ✔️ Detailed Explaination ✔️ | HashSet + DFS | 2 Solution | beats-9892-proof-detailed-explaination-h-ml0l | IntuitionThe problem requires us to recover acontaminated binary treewhere all node values are initially-1. Werestore the tree using a simple formula: the root | ntrcxst | NORMAL | 2025-02-21T03:31:02.159681+00:00 | 2025-02-21T03:31:02.159681+00:00 | 156 | false | ---

---
# Intuition
The problem requires us to recover a **contaminated binary tree** where all node values are initially `-1`. We **restore the tree using a simple formula**: the root is assigned `0`, t... | 4 | 0 | ['Array', 'Hash Table', 'String', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Recursion', 'Binary Tree', 'C++', 'Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Easy Python Solution | Faster than 99% (76 ms) | Comments | easy-python-solution-faster-than-99-76-m-b96t | Easy Python Solution | Faster than 99% (76 ms) | With Comments\n\nRuntime: 76 ms, faster than 99.13% of Python3 online submissions for Find Elements in a Contam | the_sky_high | NORMAL | 2022-05-10T13:51:21.930895+00:00 | 2022-05-10T14:06:05.483802+00:00 | 478 | false | # Easy Python Solution | Faster than 99% (76 ms) | With Comments\n\n**Runtime: 76 ms, faster than 99.13% of Python3 online submissions for Find Elements in a Contaminated Binary Tree.\nMemory Usage: 17.9 MB**\n\n```\nclass FindElements:\n \n def dfs(self, node, val):\n if node:\n self.store[val]... | 4 | 0 | ['Depth-First Search', 'Recursion', 'Python', 'Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | Java Easiest Cosise Beginer Friendly (Slow) | java-easiest-cosise-beginer-friendly-slo-8la7 | \nclass FindElements {\n TreeNode root;\n public FindElements(TreeNode r) {\n root = r;\n r.val = 0;\n }\n public boolean find(int tar | bharat194 | NORMAL | 2022-04-02T12:19:11.283049+00:00 | 2022-04-02T12:19:11.283087+00:00 | 412 | false | ```\nclass FindElements {\n TreeNode root;\n public FindElements(TreeNode r) {\n root = r;\n r.val = 0;\n }\n public boolean find(int target) {\n return find(root,target);\n }\n public boolean find(TreeNode root,int target){\n if(root == null) return false;\n if(root... | 4 | 0 | ['Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | C++ code using BFS | O(n) | c-code-using-bfs-on-by-mayank01ms-6uj5 | \nclass FindElements {\n unordered_map<int, int> m;\npublic:\n FindElements(TreeNode* root) {\n queue<TreeNode*> q;\n root->val = 0;\n | mayank01ms | NORMAL | 2021-10-19T16:52:37.325803+00:00 | 2021-10-19T16:52:37.325863+00:00 | 256 | false | ```\nclass FindElements {\n unordered_map<int, int> m;\npublic:\n FindElements(TreeNode* root) {\n queue<TreeNode*> q;\n root->val = 0;\n q.push(root);\n m[0]++;\n while(!q.empty()){\n auto temp = q.front();\n q.pop();\n m[temp->val]++;\n ... | 4 | 0 | [] | 0 |
find-elements-in-a-contaminated-binary-tree | Clean Easy Understand Python | clean-easy-understand-python-by-nhan99dn-kiat | \tdef init(self, root: TreeNode):\n root.val = 0\n self.s = set()\n q = [(root, 0)]\n self.s.add(0)\n while len(q) > 0:\n | nhan99dn | NORMAL | 2019-11-28T03:28:40.829126+00:00 | 2019-11-28T03:28:40.829161+00:00 | 421 | false | \tdef __init__(self, root: TreeNode):\n root.val = 0\n self.s = set()\n q = [(root, 0)]\n self.s.add(0)\n while len(q) > 0:\n t,v = q.pop()\n if t.left:\n q.append((t.left, v * 2 + 1))\n self.s.add(v * 2 + 1)\n if t.right:... | 4 | 0 | [] | 0 |
find-elements-in-a-contaminated-binary-tree | C# Solution for Find Elements In A Contaminated Binary Tree Problem | c-solution-for-find-elements-in-a-contam-hnyv | IntuitionThe problem involves a contaminated binary tree where all node values are initially -1, and we need to recover the original values using a fixed formul | Aman_Raj_Sinha | NORMAL | 2025-02-21T20:43:43.958887+00:00 | 2025-02-21T20:43:43.958887+00:00 | 27 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves a contaminated binary tree where all node values are initially -1, and we need to recover the original values using a fixed formula:
• Left child: 2 * x + 1
• Right child: 2 * x + 2
Since the tree follows a strict nume... | 3 | 0 | ['C#'] | 0 |
find-elements-in-a-contaminated-binary-tree | JAVA | java-by-ayeshakhan7-1enz | Code | ayeshakhan7 | NORMAL | 2025-02-21T16:56:20.968159+00:00 | 2025-02-21T16:56:20.968159+00:00 | 65 | false |
# Code
```java []
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.lef... | 3 | 0 | ['Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | DFS and Unordered Set || beats 100% | dfs-and-unordered-set-beats-100-by-aksha-lbik | IntuitionApproachComplexity
Time complexity: O(N)
Space complexity: O(N)
Code | akshatchawla1307 | NORMAL | 2025-02-21T13:34:51.935541+00:00 | 2025-02-21T13:39:32.110888+00:00 | 59 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->... | 3 | 0 | ['Depth-First Search', 'C++'] | 1 |
find-elements-in-a-contaminated-binary-tree | Simple intuitive, beats 100%, O(1) time for FindElements, O(log(n)) time for Find and O(1) space | simple-intuitive-beats-100-o1-time-for-f-2rg0 | IntuitionWe can use binary representation oftargetto go through the tree.ApproachThe key idea is usingtargetbinary representation to understand where to go in t | pavelavsenin | NORMAL | 2025-02-21T12:50:42.477351+00:00 | 2025-02-21T12:50:42.477351+00:00 | 66 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We can use binary representation of `target` to go through the tree.
# Approach
<!-- Describe your approach to solving the problem. -->
The key idea is using `target` binary representation to understand where to go in the tree to the left ... | 3 | 0 | ['Go'] | 0 |
find-elements-in-a-contaminated-binary-tree | ✅ Three Simple Lines of Code | three-simple-lines-of-code-by-mikposp-u985 | (Disclaimer: this is not an example to follow in a real project - it is written for fun and training mostly)Code #1.1Time complexity:O(n2). Space complexity:O(n | MikPosp | NORMAL | 2025-02-21T10:02:18.114669+00:00 | 2025-02-21T10:02:56.364819+00:00 | 175 | false | (Disclaimer: this is not an example to follow in a real project - it is written for fun and training mostly)
# Code #1.1
Time complexity: $$O(n^2)$$. Space complexity: $$O(n)$$.
```python3
FindElements = type('',(),{
'__init__': lambda s,r:setattr(s,'d',(f:=lambda n,x:n and {x}|f(n.left,x*2+1)|f(n.right,x*2+2) or ... | 3 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Design', 'Binary Tree', 'Python', 'Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | ✅✅Beats 100%🔥C++🔥Python|| 🚀🚀Super Simple and Efficient Solution🚀🚀||🔥Python🔥C++✅✅ | beats-100cpython-super-simple-and-effici-fdh9 | Complexity
Time complexity:O(n)
Space complexity:O(n)
Code | shobhit_yadav | NORMAL | 2025-02-21T02:49:25.405665+00:00 | 2025-02-21T02:49:25.405665+00:00 | 130 | false | # Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class FindElements {
public:
FindElements(TreeNode* root) {
dfs(root, 0);
}
bool find(int target) {
return ... | 3 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Design', 'Binary Tree', 'C++', 'Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | 🔥 Space: O(log n) | Time: O(log n) 🚀💯 Beats 99% in both Space & Time ⚡🏆 Unique Bitmask | space-olog-n-time-olog-n-beats-99-in-bot-h53p | IntuitionBeacause of the fixed way of numbering in the binary tree, there is a unique identifier for each node, represented by the value of it :-These numbers a | vkhantwal999 | NORMAL | 2025-02-21T02:12:12.645997+00:00 | 2025-02-21T02:16:50.043984+00:00 | 231 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Beacause of the fixed way of numbering in the binary tree, there is a unique identifier for each node, represented by the value of it :-
1
/ \
2 3
/ \
4 5
These numbers are the values... | 3 | 0 | ['String', 'Binary Tree', 'Bitmask', 'Java'] | 2 |
find-elements-in-a-contaminated-binary-tree | "⚡ Fast Java & C++Solution to Recover Binary Tree & Efficient Find Operation 🌳"| DFS Approach | fast-java-csolution-to-recover-binary-tr-9b2i | IntuitionThe problem asks us to recover a binary tree where some of the nodes may have corrupted values (i.e., they are initially set to -1). We're given that t | ksheker | NORMAL | 2025-02-21T02:05:11.101237+00:00 | 2025-02-21T02:05:11.101237+00:00 | 7 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem asks us to recover a binary tree where some of the nodes may have corrupted values (i.e., they are initially set to -1). We're given that the tree's structure is still intact, and the root value should be 0. The values of the ot... | 3 | 0 | ['C++', 'Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | [Rust] First thought: easy DFS + HashSet | rust-first-thought-easy-dfs-hashset-by-d-tv64 | Complexity
Time complexity:
FindElements:O(n), wherenis the size of the tree.
find:O(1)
Space complexity:
O(n)
Code | discreaminant2809 | NORMAL | 2025-02-21T01:06:53.759824+00:00 | 2025-02-21T01:06:53.759824+00:00 | 81 | false | # Complexity
- Time complexity:
`FindElements`: $$O(n)$$, where $$n$$ is the size of the tree.
`find`: $$O(1)$$
- Space complexity:
$$O(n)$$
# Code
```rust []
struct FindElements {
values: HashSet<i32>,
}
macro_rules! node_as_ref {
($node:expr) => {
$node
.as_ref()
.map(|root|... | 3 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Binary Tree', 'Rust'] | 1 |
find-elements-in-a-contaminated-binary-tree | 🚀 Easy | Tree | Set | Map | JavaScript Solution | easy-tree-set-map-javascript-solution-by-7j57 | Approach / Intuition:
Recovering the Tree:
We traverse the tree in a preorder fashion (Root → Left → Right).
We update each node's value based on its parent's | dharmaraj_rathinavel | NORMAL | 2025-02-21T00:33:03.035374+00:00 | 2025-02-21T00:34:06.790286+00:00 | 105 | false | ## Approach / Intuition:
1. **Recovering the Tree:**
- We traverse the tree in a preorder fashion (Root → Left → Right).
- We update each node's value based on its parent's value.
- We store the recovered values in a `Set` (`this.indices`) for quick lookup in `O(1)` time.
2. **Finding a Target Value:**
... | 3 | 0 | ['Hash Table', 'Tree', 'Binary Tree', 'Ordered Set', 'JavaScript'] | 0 |
find-elements-in-a-contaminated-binary-tree | DFS + Set/Map Solution - Easy_Beginner-Friendly Sol. | bfs-setmap-solution-easy_beginner-friend-rms0 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | iitian_010u | NORMAL | 2025-02-21T00:27:05.386405+00:00 | 2025-02-21T20:54:59.681071+00:00 | 161 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 3 | 0 | ['Hash Table', 'Tree', 'Breadth-First Search', 'Design', 'Binary Tree', 'Ordered Set', 'C++'] | 2 |
find-elements-in-a-contaminated-binary-tree | 🔥🔥||Easy Solution||Beats 100% in most||With proof||💎☕🖥️🐍C#️⃣C➕➕✅||🔥🔥 | easy-solutionbeats-100-in-mostwith-proof-53ee | Intuition💡It's obvious to use BFS for the initial part. However, a lot of people use HashSet(set() in python) to pre-store all the values in the initial part, w | AbyssReaper | NORMAL | 2025-02-16T06:14:33.882293+00:00 | 2025-02-16T06:14:33.882293+00:00 | 51 | false | # Intuition💡
It's obvious to use `BFS` for the initial part. However, a lot of people use HashSet(`set()` in python) to pre-store all the values in the initial part, which may cause MLE when the values are huge. There is a special way to implement `find()` that costs O(1) in space and O(logn) in time.
# Approach🚀
- ... | 3 | 0 | ['C++'] | 1 |
find-elements-in-a-contaminated-binary-tree | Python | [EXPLAINED] | tree traversal | python-explained-tree-traversal-by-diwak-q8nm | We can use any tree traversal method to make the tree contaminated, here we are using preorder traversal it is more favorable here since value of child nodes ar | diwakar_4 | NORMAL | 2022-09-07T09:06:58.288631+00:00 | 2022-10-01T12:38:48.484902+00:00 | 308 | false | * We can use any tree traversal method to make the tree contaminated, here we are using preorder traversal it is more favorable here since value of child nodes are dependent on the value of thier parent node.\n* For efficient target searching we will maintain a set and keep adding the values in it whenever we modify no... | 3 | 0 | ['Python'] | 0 |
find-elements-in-a-contaminated-binary-tree | Simple and clean C++ solution|| Using DFS and a HashSet | simple-and-clean-c-solution-using-dfs-an-4doa | If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries | anubhavbaner7 | NORMAL | 2022-06-20T20:17:03.875530+00:00 | 2022-06-20T20:17:03.875566+00:00 | 420 | false | **If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries or some improvements please feel free to comment and share your views.**\n\n```\n unordered_set<int> s;\n void solve(TreeNode* root)\n {\n if(... | 3 | 0 | ['Depth-First Search', 'C', 'C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | Beginner friendly Python Soluiton | beginner-friendly-python-soluiton-by-him-ghlk | Time Complexity : O(N)\n\n# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, val=0, left=None, right=None):\n# se | HimanshuBhoir | NORMAL | 2022-02-15T04:56:14.605204+00:00 | 2022-02-15T04:56:14.605242+00:00 | 175 | false | **Time Complexity : O(N)**\n```\n# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass FindElements(object):\n\n def __init__(self, root):\n self.st = s... | 3 | 0 | ['Python'] | 0 |
find-elements-in-a-contaminated-binary-tree | C++ - O(n) to build & O(1) query | c-on-to-build-o1-query-by-anandman03-uync | \nclass FindElements {\nprivate:\n unordered_set<int> cache;\n \n void recover(TreeNode *root, int prev)\n {\n if(!root) return;\n \n | anandman03 | NORMAL | 2020-11-03T10:10:59.313177+00:00 | 2020-11-03T10:12:22.942450+00:00 | 484 | false | ```\nclass FindElements {\nprivate:\n unordered_set<int> cache;\n \n void recover(TreeNode *root, int prev)\n {\n if(!root) return;\n \n root->val = prev;\n cache.insert(root->val);\n \n recover(root->left, 2*prev + 1);\n recover(root->right, 2*prev + 2);\n ... | 3 | 0 | ['C'] | 0 |
find-elements-in-a-contaminated-binary-tree | Easy C++ Solution using HashMap | easy-c-solution-using-hashmap-by-vishal_-q1lt | \nclass FindElements\n{\npublic:\n unordered_map<int,int>m;\n\n FindElements(TreeNode* root)\n {\n if(root)\n {\n root->val = | vishal_rajput | NORMAL | 2020-07-23T07:21:54.079354+00:00 | 2020-07-23T07:21:54.079404+00:00 | 261 | false | ```\nclass FindElements\n{\npublic:\n unordered_map<int,int>m;\n\n FindElements(TreeNode* root)\n {\n if(root)\n {\n root->val = 0;\n queue<TreeNode*> q;\n q.push(root);\n m[0]++;\n\n while(!q.empty())\n {\n TreeNode... | 3 | 0 | ['Breadth-First Search', 'C'] | 0 |
find-elements-in-a-contaminated-binary-tree | [C/C++ Solution] The find is O(1), uses a bitset instead of a set (saves memory) | cc-solution-the-find-is-o1-uses-a-bitset-7dw8 | If the tree is not too sparse, using a bitset uses less memory than an unordered_set. I think it amounts to only 8KiB, given the problem\'s upper node limit, en | bogmihdav | NORMAL | 2020-02-15T17:38:48.509063+00:00 | 2020-02-15T17:38:48.509101+00:00 | 323 | false | If the tree is not too sparse, using a bitset uses less memory than an unordered_set. I think it amounts to only 8KiB, given the problem\'s upper node limit, enough to fit entirely in the CPU\'s cache. I also expect element access to compare favorably with an unordered_set\'s.\nIf lots of small trees are to be expected... | 3 | 0 | ['C'] | 0 |
find-elements-in-a-contaminated-binary-tree | C++/Java hash set, O(1) | cjava-hash-set-o1-by-votrubac-kxh4 | Iterative approch to resolve tree values and populate hash map.\n\nC++\nCPP\nunordered_set<int> s;\nFindElements(TreeNode* root) {\n queue<pair<TreeNode*, in | votrubac | NORMAL | 2019-11-17T04:35:22.073502+00:00 | 2019-11-17T04:55:21.572162+00:00 | 227 | false | Iterative approch to resolve tree values and populate hash map.\n\n**C++**\n```CPP\nunordered_set<int> s;\nFindElements(TreeNode* root) {\n queue<pair<TreeNode*, int>> q;\n q.push({root, 0});\n while (!q.empty()) {\n auto p = q.front(); q.pop();\n if (p.first != nullptr) {\n s.insert(p... | 3 | 3 | [] | 0 |
find-elements-in-a-contaminated-binary-tree | Simple CPP solution | simple-cpp-solution-by-aadithya18-mior | CodeThank you,
aadithya18. | aadithya18 | NORMAL | 2025-02-24T14:15:29.933893+00:00 | 2025-02-24T17:30:06.014285+00:00 | 10 | false |
# Code
```cpp []
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode ... | 2 | 0 | ['C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | Python Solution : DFS + Hashmap | python-solution-dfs-hashmap-by-apurv_sj-7qxr | Intuition
Do a DFS on tree
Get all values in a hashmap
Complexity
Time complexity:0(N)
Space complexity:O(N)
Code | apurv_sj | NORMAL | 2025-02-22T09:48:22.818409+00:00 | 2025-02-22T09:48:22.818409+00:00 | 6 | false | # Intuition
1. Do a DFS on tree
2. Get all values in a hashmap
# Complexity
- Time complexity:
0(N)
- Space complexity:
O(N)
# Code
```python3 []
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# ... | 2 | 0 | ['Hash Table', 'Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | Different approach than sets | O(1) Space optimized| O(log(k) Time complexity for each query | different-approach-than-sets-o1-space-op-2m15 | IntuitionFew observations that can be made here:
All the odd numbers will be on the left child to a parent node and all the even numbers will be on right child | parikshit_ | NORMAL | 2025-02-21T18:15:51.286634+00:00 | 2025-02-21T18:18:14.387349+00:00 | 18 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Few observations that can be made here:
1. All the odd numbers will be on the left child to a parent node and all the even numbers will be on right child to a parent.
2. To calculate child, we are given this formula: n1 = 2x + 1 and n2 = 2... | 2 | 0 | ['Design', 'Binary Tree', 'Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | 🔥Beats 100% | Easy for Beginners | ✨ 2 Approaches | Java | C++ | beats-100-easy-for-beginners-2-approache-h1xz | IntuitionAn Contaminated binary tree is given, where each node value is corrupted. We need to recover the tree by following conditions ->
The root node value is | panvishdowripilli | NORMAL | 2025-02-21T15:54:26.514151+00:00 | 2025-02-21T15:54:26.514151+00:00 | 26 | false |

# Intuition
An Contaminated binary tree is given, where each node value is corrupted. We need to recover the tree by following conditions ->
1. The root node value is set to `0`.
... | 2 | 0 | ['Hash Table', 'Depth-First Search', 'Recursion', 'Binary Tree', 'C++', 'Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | EASY 2 APPROACHES || BFS, HASHING | easy-2-approaches-bfs-hashing-by-sarvpre-bov8 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Sarvpreet_Kaur | NORMAL | 2025-02-21T15:28:00.977559+00:00 | 2025-02-21T15:28:00.977559+00:00 | 19 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 2 | 0 | ['Hash Table', 'Tree', 'Breadth-First Search', 'Design', 'Binary Tree', 'C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | O(n) python solution with step by step explanation | on-python-solution-with-step-by-step-exp-7jml | IntuitionWe can solve this problem using both bfs and dfs but we will solve it using dfs. Because we have to solve it in top down approach as to find a child no | 9chaitanya11 | NORMAL | 2025-02-21T13:53:20.991621+00:00 | 2025-02-21T13:53:20.991621+00:00 | 18 | false | # Intuition
We can solve this problem using both bfs and dfs but we will solve it using dfs. Because we have to solve it in top down approach as to find a child node's value, we must find the parent node's value first. Tree traversal is done in many types but we need preorder traversal in this case as it says we must f... | 2 | 0 | ['Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | simple py solution - beats 95% | simple-py-solution-beats-95-by-noam971-23at | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | noam971 | NORMAL | 2025-02-21T13:37:55.102477+00:00 | 2025-02-21T13:37:55.102477+00:00 | 15 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 2 | 0 | ['Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | 🚀 Optimized Implicit Reconstruction || 0ms in Rust & Go || O(log target) Time and Space | optimized-recursive-tree-navigation-impl-0t22 | Intuition 🤔Instead of explicitly reconstructing the tree, we leverage the given rules torecursively navigateand determine iftargetexists.Each node’s value follo | rutsh | NORMAL | 2025-02-21T09:48:52.998458+00:00 | 2025-02-21T11:44:57.638974+00:00 | 101 | false | # Intuition 🤔
Instead of explicitly reconstructing the tree, we leverage the given rules to __recursively navigate__ and determine if `target` exists.
Each node’s value follows a strict formula, so we can compute its __parent's value__ and check if the target node should exist.
# Approach 🔍
- __Recursive Parent L... | 2 | 0 | ['Recursion', 'Binary Tree', 'Go', 'Rust', 'Racket'] | 0 |
find-elements-in-a-contaminated-binary-tree | C++ Simple and Easy to Understand Recursive Solution | c-simple-and-easy-to-understand-recursiv-wxuu | IntuitionRecover the tree recursively while keeping all the values in a hashmap to find them easily.Complexity
Time complexity:Recover tree - O(n)Find - O(1) | yehudisk | NORMAL | 2025-02-21T08:54:31.713513+00:00 | 2025-02-21T08:54:31.713513+00:00 | 6 | false | # Intuition
Recover the tree recursively while keeping all the values in a hashmap to find them easily.
# Complexity
- Time complexity:
Recover tree - O(n)
Find - O(1)
- Space complexity: O(n)
# Code
```cpp []
class FindElements {
public:
unordered_set<int> values;
void recover(TreeNode* root) {
if ... | 2 | 0 | ['C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | ✅ Easy to Understand | Beginner Friendly | Tree | Design | Hash Table | Detailed Video Explanation🔥 | easy-to-understand-beginner-friendly-tre-kczv | IntuitionThe problem involves reconstructing a contaminated binary tree where all values were replaced with-1. Given a root node, we need to recover the tree fo | sahilpcs | NORMAL | 2025-02-21T08:43:13.716161+00:00 | 2025-02-21T08:43:13.716161+00:00 | 30 | false | # Intuition
The problem involves reconstructing a contaminated binary tree where all values were replaced with `-1`. Given a root node, we need to recover the tree following a specific formula:
- The root node is assigned a value of `0`.
- The left child of a node with value `val` is assigned `2 * val + 1`.
- The right... | 2 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Design', 'Binary Tree', 'Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Kotlin. Beats 100% (66 ms). Using IntArray as a stack to build a path | kotlin-beats-100-71-ms-using-linkedlisti-od3f | CodeApproachLet's build a full new tree for the first elements from 0 to 14:We can see that there is some pattern in the elements.Odd elements are always to the | mobdev778 | NORMAL | 2025-02-21T07:51:43.129385+00:00 | 2025-02-21T10:19:51.347831+00:00 | 35 | false | 
# Code
```kotlin []
class FindElements(val root: TreeNode?) {
val path = IntArray(100)
var size = 0
fun find(target: Int): Boolean {
size = 0
findPath(target)
var no... | 2 | 0 | ['Kotlin'] | 1 |
find-elements-in-a-contaminated-binary-tree | Simple | Stack | Log(n) | Readable | simple-stack-logn-readable-by-apakg-itvt | Code: Approach 1: Using Stack.Complexity
Time complexity: O(logn)
Space complexity: O(logn)
Approach 2: BFS. | Apakg | NORMAL | 2025-02-21T07:46:28.677044+00:00 | 2025-02-21T07:46:28.677044+00:00 | 15 | false | # Code: Approach 1: Using Stack.
```java []
class FindElements {
TreeNode root;
public FindElements(TreeNode root) {
this.root = root;
}
public boolean find(int target) {
Stack<Boolean> stack = new Stack<>();
for(; target != 0; target = (--target) >> 1) {
stack.p... | 2 | 0 | ['Stack', 'Tree', 'Breadth-First Search', 'Monotonic Stack', 'Binary Tree', 'Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Easy code, must try in java | easy-code-must-try-in-java-by-notaditya0-jocd | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | NotAditya09 | NORMAL | 2025-02-21T07:14:12.189502+00:00 | 2025-02-21T07:14:12.189502+00:00 | 54 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 2 | 0 | ['Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Most intuitive solution, Easy understanding. Beats 100%😎😎🔥🔥 | most-intuitive-solution-easy-understandi-bwnz | IntuitionThe problem states that the binary tree has been "contaminated," meaning all values are -1. However, we are given a specific set of transformation rule | nnzl48NW9N | NORMAL | 2025-02-21T05:38:16.493692+00:00 | 2025-02-21T05:38:16.493692+00:00 | 30 | false | # Intuition
The problem states that the binary tree has been "contaminated," meaning all values are -`1`. However, we are given a specific set of transformation rules that determine the correct values for each node based on its parent.
- Since the root is `0`, we can reconstruct the entire tree using **DFS (Depth-Firs... | 2 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Design', 'Binary Tree', 'C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | Easy to understand and Easy Approach(Beat 80%) 😊😊 | easy-to-understand-and-easy-approachbeat-voa1 | IntuitionThe problem is about reconstructing a contaminated binary tree where all values are initially set to -1. We are given the root node of this tree, and w | patelaviral | NORMAL | 2025-02-21T04:37:03.102276+00:00 | 2025-02-21T04:37:43.242075+00:00 | 9 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem is about reconstructing a contaminated binary tree where all values are initially set to -1. We are given the root node of this tree, and we need to recover the original values following a specific rule:
* The root node is assi... | 2 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Design', 'Binary Tree', 'Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Finding Elements in a Contaminated Binary Tree || DFS || JAVA ✅ | recover-tree-java-by-faiz_rahman_g-mx77 | IntuitionThe problem involves recovering a corrupted binary tree where each node's value was replaced with -1. We need to reconstruct the values such that:The r | Faiz_Rahman_G | NORMAL | 2025-02-21T04:34:56.613723+00:00 | 2025-02-21T06:18:07.984284+00:00 | 70 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves recovering a corrupted binary tree where each node's value was replaced with -1. We need to reconstruct the values such that:
The root is 0.
The left child of a node with value x has value 2*x + 1.
The right child of a... | 2 | 0 | ['Tree', 'Depth-First Search', 'Binary Tree', 'Java'] | 1 |
find-elements-in-a-contaminated-binary-tree | Hash Set and Depth-First Search | hash-set-and-depth-first-search-by-khale-6rqp | Complexity
Time complexity:
O(n)fornew FindElements(root)
O(1)forFindElements.find(target)
Space complexity:
O(n)
Code | khaled-alomari | NORMAL | 2025-02-21T04:24:31.437079+00:00 | 2025-02-21T05:31:48.184013+00:00 | 76 | false | # Complexity
- Time complexity:
$$O(n)$$ for `new FindElements(root)`
$$O(1)$$ for `FindElements.find(target)`
- Space complexity:
$$O(n)$$
# Code
```typescript []
class FindElements {
private set = new Set<number>();
constructor(root: TreeNode | null) {
const dfs = (node: TreeNode | null, val: number... | 2 | 0 | ['Hash Table', 'Math', 'Tree', 'Depth-First Search', 'Design', 'Simulation', 'Binary Tree', 'TypeScript', 'JavaScript'] | 0 |
find-elements-in-a-contaminated-binary-tree | JAVA SOLUTION || BFS | java-solution-bfs-by-gunasaihari_krishna-ujmt | Code | gunasaihari_krishna-2004 | NORMAL | 2025-02-21T03:54:08.119912+00:00 | 2025-02-21T03:54:08.119912+00:00 | 25 | false | # Code
```java []
class FindElements {
HashSet<Integer> hm=new HashSet<>();
public FindElements(TreeNode root) {
root.val=0;
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
hm.add(0);
while(!q.isEmpty())
{
TreeNode node=q.remove();
if(... | 2 | 0 | ['Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Simple C++ Solution | Binary Tree | InOrder Traversal | simple-c-solution-binary-tree-inorder-tr-tgio | SolutionComplexity
Time complexity:FindElements:O(n)find:O(1)
Space complexity:FindElements:O(n)find:O(1)
Code | ipriyanshi | NORMAL | 2025-02-21T03:39:24.990023+00:00 | 2025-02-21T03:39:24.990023+00:00 | 96 | false | # Solution
https://youtu.be/MzMYRFmV-PA
# Complexity
- Time complexity:
FindElements: $$O(n)$$
find: $$O(1)$$
- Space complexity:
FindElements: $$O(n)$$
find: $$O(1)$$
# Code
```cpp []
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* ... | 2 | 0 | ['C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | BFS Traversal || SImple Solution || O(N) Time Complexity || CPP | bfs-traversal-simple-solution-on-time-co-klp5 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Heisenberg_wc | NORMAL | 2025-02-21T03:30:18.550928+00:00 | 2025-02-21T03:30:18.550928+00:00 | 57 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 2 | 0 | ['C++'] | 0 |
find-elements-in-a-contaminated-binary-tree | Javascript | javascript-by-davidchills-ub3c | Code | davidchills | NORMAL | 2025-02-21T03:03:46.147453+00:00 | 2025-02-21T03:03:46.147453+00:00 | 85 | false | # Code
```javascript []
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
*/
var FindElements ... | 2 | 0 | ['JavaScript'] | 0 |
find-elements-in-a-contaminated-binary-tree | Easy Java Solution Using Set And BFS | easy-java-solution-using-set-and-bfs-by-2zbn8 | IntuitionAs Statement says just keep goingApproachWork on the BFS and Trust on Recursion.Using a helper Method in which i am updating the child node values one | shbhm20 | NORMAL | 2025-02-21T02:50:42.101730+00:00 | 2025-02-21T02:50:42.101730+00:00 | 78 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
As Statement says just keep going
# Approach
<!-- Describe your approach to solving the problem. -->
Work on the BFS and Trust on Recursion.
Using a helper Method in which i am updating the child node values one by one.
# Complexity
- Tim... | 2 | 0 | ['Hash Table', 'Tree', 'Breadth-First Search', 'Recursion', 'Simulation', 'Binary Tree', 'Hash Function', 'Ordered Set', 'Java'] | 2 |
find-elements-in-a-contaminated-binary-tree | 🥇EASY || C++ || BEGINNER FRIENDLY || QUEUE || LEVEL ORDER TRAVERSAL || MAP ✅✅ | easy-c-beginner-friendly-queue-level-ord-obdu | Code | kruppatel64 | NORMAL | 2025-02-21T01:52:25.287248+00:00 | 2025-02-21T02:39:52.221955+00:00 | 81 | false | # Code
```cpp []
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *r... | 2 | 0 | ['Hash Table', 'Tree', 'Queue', 'Binary Tree', 'C++'] | 1 |
find-elements-in-a-contaminated-binary-tree | 1261. Find Elements in a Contaminated Binary Tree | 1261-find-elements-in-a-contaminated-bin-5ahe | IntuitionApproachComplexity
Time complexity:
Space complexity:
Can give a upvote and support 🔥Code | akashdoss996 | NORMAL | 2025-02-21T01:26:22.951422+00:00 | 2025-02-21T01:26:22.951422+00:00 | 118 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Can gi... | 2 | 0 | ['Python', 'Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | ✅Simple BFS || O(N) Efficient Solution. | simple-bfs-on-efficient-solution-by-pras-qc58 | IntuitionApproachComplexity
Time complexity: O(N)
Space complexity: O(N)
Code | prashanth_d4 | NORMAL | 2025-02-21T00:47:25.288228+00:00 | 2025-02-21T00:48:06.306729+00:00 | 75 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->... | 2 | 0 | ['Hash Table', 'Tree', 'Breadth-First Search', 'Design', 'Binary Tree', 'Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Beats 99%. Dawgs ts is heat. DFS and Set bruzz feel me? | beats-99-dawgs-ts-is-heat-dfs-and-set-br-qui2 | Code | Ishaan_P | NORMAL | 2025-02-21T00:26:39.007844+00:00 | 2025-02-21T00:26:39.007844+00:00 | 25 | false | # Code
```java []
class FindElements {
Set<Integer> found;
public FindElements(TreeNode root) {
found = new HashSet<>();
dfs(root,0);
}
public boolean find(int target) {
return found.contains(target);
}
public void dfs(TreeNode curr, int currVal){
if(curr =... | 2 | 0 | ['Java'] | 0 |
find-elements-in-a-contaminated-binary-tree | Swift💯 BitSet DFS/BFS | swift-bitset-by-upvotethispls-raal | BitSet DFS (accepted answer)BitSetref:https://swift-collections/1.1.0/documentation/bitcollections/bitsetBitSet BFS (accepted answer) | UpvoteThisPls | NORMAL | 2025-02-21T00:16:32.226685+00:00 | 2025-02-21T00:53:29.429409+00:00 | 63 | false | **BitSet DFS (accepted answer)**
```
class FindElements {
var set = BitSet()
init(_ root: TreeNode?) {
func dfs(_ node: TreeNode?, _ val:Int) {
guard let node else { return }
set.insert(val)
dfs(node.left, val*2 + 1)
dfs(node.right, val*2 + 2)
}
dfs(root, 0)
}
func find(_ tar... | 2 | 0 | ['Swift'] | 1 |
find-elements-in-a-contaminated-binary-tree | BFS + Set Solution - Beats 93% | bfs-set-solution-beats-93-by-richyrich20-w0qi | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | RichyRich2004 | NORMAL | 2025-02-21T00:13:50.225252+00:00 | 2025-02-21T00:17:18.673329+00:00 | 169 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 2 | 0 | ['Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | Easy python O(log target) solution with O(1) space [binary search] | easy-python-olog-target-solution-with-o1-x4pk | IntuitionThe solution is inspired from the binary search method followed in https://leetcode.com/problems/count-complete-tree-nodes/editorialApproachWhen the no | kowshika95 | NORMAL | 2025-02-13T19:24:01.200836+00:00 | 2025-02-13T19:24:01.200836+00:00 | 230 | false | # Intuition
The solution is inspired from the binary search method followed in https://leetcode.com/problems/count-complete-tree-nodes/editorial
# Approach
When the nodes are numbered from 1 to n in a binary tree, we can check the existence of any node using the technique similar to binary search. This would take O(l... | 2 | 0 | ['Binary Search', 'Binary Tree', 'Python3'] | 0 |
find-elements-in-a-contaminated-binary-tree | 🌳57. 2 Solutions || Recursive [ DFS ] || Magic 🪄 || Short & Sweet Code !! || C++ Code Reference !! | 57-2-solutions-recursive-dfs-magic-short-t76l | \n# Code\ncpp [Using Set ]\n\nclass FindElements {\n\n unordered_set<int>s;\npublic:\n FindElements(TreeNode* a) { Traverse(a,0); }\n \n bool find(i | Amanzm00 | NORMAL | 2024-07-10T10:17:42.846100+00:00 | 2024-07-10T10:17:42.846169+00:00 | 104 | false | \n# Code\n```cpp [Using Set ]\n\nclass FindElements {\n\n unordered_set<int>s;\npublic:\n FindElements(TreeNode* a) { Traverse(a,0); }\n \n bool find(int T) { return s.count(T)>0;}\n\nprivate: \n void Traverse(TreeNode* a,int x)\n {\n if(!a) return;\n\n a->val=x;\n s.insert(x);\n ... | 2 | 0 | ['Hash Table', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Design', 'Recursion', 'Binary Tree', 'C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.