1110.删点成林

Ice_and_Fire Ice_and_Fire     2022-11-30     424

关键词:

给出二叉树的根节点 root,树上每个节点都有一个不同的值。

如果节点值在 to_delete 中出现,我们就把该节点从树上删去,最后得到一个森林(一些不相交的树构成的集合)。

返回森林中的每棵树。你可以按任意顺序组织答案。

 

示例:

 

输入:root = [1,2,3,4,5,6,7], to_delete = [3,5]
输出:[[1,2,null,4],[6],[7]]
 

提示:

树中的节点数最大为 1000。
每个节点都有一个介于 1 到 1000 之间的值,且各不相同。
to_delete.length <= 1000
to_delete 包含一些从 1 到 1000、各不相同的值。

/**
 * 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 *right) : val(x), left(left), right(right) 
 * ;
 */
class Solution 
public:
    vector<TreeNode*> res;
    set<int> delSet;
    TreeNode* delNodesProcess(TreeNode* root)
    
        if(root == nullptr)
        
            return nullptr;
        

        root->left = delNodesProcess(root->left);
        root->right = delNodesProcess(root->right);
        if(delSet.find(root->val) != delSet.end())
        
            if(root->left !=nullptr)
            
                res.push_back(root->left);
            
            if(root->right !=nullptr)
            
                res.push_back(root->right);
            
            root = nullptr;
        
        return root;
    
    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) 
        if(root == nullptr)
        
            return res;
        
        int len = to_delete.size();
        for(int i = 0; i < len; i++)
        
            delSet.insert(to_delete[i]);
        
        if(root == nullptr)
        
            return res;
        

        root->left = delNodesProcess(root->left);
        root->right = delNodesProcess(root->right);
        if(delSet.find(root->val) != delSet.end())
        
            if(root->left !=nullptr)
            
                res.push_back(root->left);
            
            if(root->right !=nullptr)
            
                res.push_back(root->right);
            
        
        else
        
            res.push_back(root);
        
        return res;
    
;

  

hdu2473junk-mailfilter删点并查集

删点并查集,就是用一个新的点标取代之前的点标就可以。。y一下就能够了#include<cstdio>#include<iostream>#include<algorithm>#include<string.h>#include<vector>#include<map>#include<queue>#include& 查看详情

可以删点的并查集

如题。方法一:LCT!细节挺多,略。方法二:如题(废话。。)如果照传统的方法,比如1,2,3在一起要把1删掉,要保证1的爸爸和2,3以后不一样,如果1不是根节点就直接$fa[1]=1$,否则需要改所有的儿子。上面的问题在于删掉... 查看详情

nyoj_1022:合纵连横(并查集删点)

http://acm.nyist.net/JudgeOnline/problem.php?pid=1022只附代码好了#include<bits/stdc++.h>usingnamespacestd;constintN=200005;inta[N],b[N],vis[N];intn,m,add,kase;voidinit(){for(inti=0;i<n;i++)a[i]=i,b 查看详情

hdu2473junk-mailfilter并查集删点,模板题(代码片段)

TimeLimit:15000/8000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):10376    AcceptedSubmission(s):3280ProblemDescriptionRecognizingj 查看详情

p2661信息传递删点+dfs(代码片段)

题目描述有$n$个同学(编号为$1$到$n$)正在玩一个信息传递的游戏。在游戏里每人都有一个固定的信息传递对象,其中,编号为$i$的同学的信息传递对象是编号为$T_i$的同学。游戏开始时,每人都只知道自己的生日。之后每一轮... 查看详情

校内模拟测试010t1删点游戏dt(代码片段)

题意简述n个点m条边的无向图,要把所有点一个一个地删去。每次删去一个点的花费为这个点相邻的还未被删除的点的点权。无重边无自环,求最小代价。数据范围对于(30\%)的数据(nle10)。对于(60\%)的数据(n,mle1000)。对于(100\%)的数... 查看详情

“建木”萌芽,聚木成林

据Github2021年度报告显示,目前Github用户数已超 7300万,中国Github开发者755万,开源吞噬世界的当下,越来越多中国开发者和企业积极参与开源建设。有一位从事开源10多年的从业人员,戏称自己为未来希望成... 查看详情

bzoj1015题解

【题意分析】  给你一张无向图,要求支持删点和询问连通块数。【解题思路】  可以直接可持久化并查集大力艹过去。  考虑到正着删点就是倒着加点,所以并不需要可持久化。复杂度O((k+m)α(n))。【参考代码】  ... 查看详情

pat1110:completebinarytree

1110.CompleteBinaryTree(25)时间限制400ms内存限制65536kB代码长度限制16000B判题程序Standard作者CHEN,YueGivenatree,youaresupposedtotellifitisacompletebinarytree.InputSpecification:Eachinputfilecontainsonetestcase.Foreachcas 查看详情

pat1110completebinarytree[比较](代码片段)

1110 CompleteBinaryTree (25分)Givenatree,youaresupposedtotellifitisacompletebinarytree.InputSpecification:Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinegivesapositiveinteger N 查看详情

engg1110—problemsolvingbyprogramming

ENGG1110—ProblemSolvingbyProgramming2019–2020Term1ProjectSpecification—AVendingMachineSimulator1.IntroductionVendingmachinesareautomatedmachinesthatprovidedifferentproductstoconsumersafterpaymentisaccepted.Forsimplicity,wewillbefocusingonvendingmachinesthattakecoins(availableinHong... 查看详情

engg1110—problemsolving

ENGG1110—ProblemSolvingbyProgramming2019–2020Term1ProjectSpecification—AVendingMachineSimulator1.IntroductionVendingmachinesareautomatedmachinesthatprovidedifferentproductstoconsumersafterpaymentisaccepted.Forsimplicity,wewillbefocusingonvendingmachinesthattakecoins(availableinHong... 查看详情

1110

 今天学习了装虚拟机,装了一个linux系统,了解了几个用法。学习了html中的超链接,锚点,target功能。笔记: linux:    关机 falu    用户名 root    查找自己的当前位置&n... 查看详情

51nod1110(xjb)

...目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1110 题意:中文题诶~ 思路:可以将在xi位置,权值为wi的点看作有wi个点在xi位置.然后再按位置排一下序,再找中位数即可; 代码:1#include<iostream>2#include... 查看详情

1110jquary动画

<script>$(function(){$("#btn").click(function(){//$("#div").hide(2000);//在2秒内隐藏//$("#div").show(2000);//在2秒内显示//$("#div").fadeIn(2000);//增强元素的不透明度,直至元素完全显示//$("#div").fadeOut(2000);//降低元素的不透明度,直 查看详情

1110最近共同祖先

题目来源:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1110Description如上图所示,由正整数1,2,3,...组成了一棵无限大的二叉树。从某一个结点到根结点(编号是1的结点)都有一条唯一的路径,比如从10到根结点的路径是(10,5,2,1),从4到根... 查看详情

为啥我得到 1、10、110、1110 等而不是 1、10、100、1000 等?

】为啥我得到1、10、110、1110等而不是1、10、100、1000等?【英文标题】:WhyamIgetting1,10,110,1110etc.ratherthan1,10,100,1000etc.?为什么我得到1、10、110、1110等而不是1、10、100、1000等?【发布时间】:2021-03-3018:46:27【问题描述】:我是C++初... 查看详情

bzoj1110:[poi2007]砝码odw

1110:[POI2007]砝码OdwTimeLimit: 10Sec  MemoryLimit: 162MBSubmit: 547  Solved: 296[Submit][Status][Discuss]Description  在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作 查看详情