力扣每日一题 在受污染的二叉树中查找元素 哈希 DFS 二进制

news/2024/7/20 21:27:06 标签: 深度优先, leetcode, 哈希算法

Problem: 1261. 在受污染的二叉树中查找元素在这里插入图片描述

思路

👨‍🏫 灵神题解

💖 二进制

  • 时间复杂度:初始化为 O ( 1 ) O(1) O(1);find 为 O ( m i n ( h , l o g 2 t a r g e t ) O(min(h,log_2target) O(min(h,log2target),其中 h 为二叉树的高度
  • 空间复杂度: O ( 1 ) O(1) O(1)
class FindElements {
    private TreeNode root;

    public FindElements(TreeNode root) {
        this.root = root;
    }

    public boolean find(int target) {
        target++;
        TreeNode cur = root; // 从根节点出发
        for (int i = 30 - Integer.numberOfLeadingZeros(target); i >= 0; i--) { // 从次高位开始枚举
            int bit = (target >> i) & 1; // target 第 i 位的比特值
            cur = bit == 0 ? cur.left : cur.right;
            if (cur == null) { // 走到空节点,说明 target 不在二叉树中
                return false;
            }
        }
        return true; // 没有走到空节点,说明 target 在二叉树中
    }
}

在这里插入图片描述

💖 哈希表

复杂度分析

  • 时间复杂度:初始化为 O ( n ) O(n) O(n),其中 n n n 为二叉树的节点个数。find 为 O ( 1 ) O(1) O(1)
  • 空间复杂度: O ( n ) O(n) O(n)
class FindElements {
    private final Set<Integer> s = new HashSet<>();

    public FindElements(TreeNode root) {
        dfs(root, 0);
    }

    public boolean find(int target) {
        return s.contains(target);
    }

    private void dfs(TreeNode node, int val) {
        if (node == null) {
            return;
        }
        s.add(val);
        dfs(node.left, val * 2 + 1);
        dfs(node.right, val * 2 + 2);
    }
}

💖 暴力版

  • 时间复杂度:
    • 转换: O ( n ) O(n) O(n)
    • 查找: O ( n m ) O(nm) O(nm)
  • 空间复杂度: O ( n ) O(n) O(n)
/**
 * 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.left = left;
 *         this.right = right;
 *     }
 * }
 */
class FindElements {
    TreeNode r;
    public FindElements(TreeNode root) {
        r = root;
        if(root == null) return;
        root.val = 0;
        dfs(root);
    }

    void dfs(TreeNode cur)
    {
        if(cur == null)
            return;
        if(cur.left != null)
        {
            cur.left.val = cur.val * 2 + 1;
            dfs(cur.left);
        }
        if(cur.right != null)
        {
            cur.right.val = cur.val * 2 + 2;
            dfs(cur.right);
        }
    }
    
    boolean f(TreeNode cur, int x)
    {
        if(cur == null)
            return false;
        if(cur.val == x)
            return true;
        if(f(cur.left,x) || f(cur.right,x))
            return true;
        return false;
    }

    public boolean find(int target) {
        return f(r,target);
    }
}

/**
 * Your FindElements object will be instantiated and called as such:
 * FindElements obj = new FindElements(root);
 * boolean param_1 = obj.find(target);
 */

http://www.niftyadmin.cn/n/5424953.html

相关文章

ByLabelText

getByLabelText, queryByLabelText, getAllByLabelText, queryAllByLabelText, findByLabelText, findAllByLabelText API getByLabelText(// If youre using screen, then skip the container argument:container: HTMLElement,text: TextMatch,options?: {selector?: stri…

ARM学习(25)链接装载高阶认识

ARM学习&#xff08;25&#xff09;链接装载高阶认识 1、例子引出 笔者先引入几个编译链接的例子来介绍一下&#xff1a; 声明无效&#xff1a;declared implicitly&#xff1f;&#xff0c;属于编译错误还是链接错误&#xff1f; 编译阶段的错误&#xff0c;属于编译错误&am…

3.8作业

1.找出来我们之前写的链表的加载和保存的代码&#xff0c;实现&#xff0c;当按 ctrl c的时候&#xff0c;保存链表 #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <s…

【Linux的shell原理 与 Linux权限】

Linux学习笔记---003 Linux的shell原理 与 Linux权限1、shell命令及运行原理1.1、shell运行原理1.2、那么外壳程序是什么&#xff1f;1.3、为什么存在外壳程序&#xff1f;1.4、单单只有外壳程序&#xff0c;对于对任务怎么办&#xff1f;1.5、shell与bash的区别 2、Linux权限2…

案例分析篇14:信息系统安全设计考点(2024年软考高级系统架构设计师冲刺知识点总结系列文章)

专栏系列文章推荐: 2024高级系统架构设计师备考资料(高频考点&真题&经验)https://blog.csdn.net/seeker1994/category_12593400.html 【历年案例分析真题考点汇总】与【专栏文章案例分析高频考点目录】(2024年软考高级系统架构设计师冲刺知识点总结-案例分析篇-…

git pull 报错: 在签出前,请清理存储库工作树

问题&#xff1a; 使用vscode 用git 拉取代码&#xff0c;提示&#xff1a;在签出前&#xff0c;请清理存储库工作树** 原因&#xff1a; git仓库上的代码和本地代码存在冲突了所以会报这个报错。 解决办法&#xff1a; ①git stash 先将本地修改存储起来 ②git pull 拉取远…

Quartz的分布式功能化设计

Quartz的分布式功能化设计 文章目录 Quartz的分布式功能化设计主体功能实现依赖API例子JOBJob记录表设计java具体代码DateDOOperatorDOSysQuartzJobDOPageDTOQuartzJobDTOQuartzJobPageDTOQuartzJobStatusEnumQuartzJobControllerIQuartzJobServiceQuartzJobServiceImplQuartzJ…

nvm 的安装与管理 node.js

文章目录 下载 nvm使用 nvm 下载与管理 node.jsnpm 切换镜像源使用 cnpm使用 yarn 下载 nvm NVM是Node.js的版本管理工具&#xff0c;它允许你轻松地在同一台机器上安装和切换不同版本的Node.js。使用NVM&#xff0c;你可以在不同的项目中使用不同的Node.js版本&#xff0c;而…