Skip to content

Files

Latest commit

Zanger67/leetcodeZanger67/leetcode
Zanger67/leetcode
and
Zanger67/leetcode
Mar 10, 2025
c2576ce · Mar 10, 2025

History

History
54 lines (39 loc) · 1.52 KB

_1261. Find Elements in a Contaminated Binary Tree.md

File metadata and controls

54 lines (39 loc) · 1.52 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : February 21, 2025

Last updated : February 21, 2025


Related Topics : Hash Table, Tree, Depth-First Search, Breadth-First Search, Design, Binary Tree

Acceptance Rate : 84.82 %


Solutions

Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class FindElements:
    def dfs(self, curr: Optional[TreeNode], curr_val: int) -> None :
        if not curr :
            return

        self.vals.add(curr_val)
        self.dfs(curr.left, curr_val * 2 + 1)
        self.dfs(curr.right, curr_val * 2 + 2)

    def __init__(self, root: Optional[TreeNode]):
        self.vals = set()
        self.dfs(root, 0)

    def find(self, target: int) -> bool:
        return target in self.vals


# Your FindElements object will be instantiated and called as such:
# obj = FindElements(root)
# param_1 = obj.find(target)