LeetCode-在受污染的二叉树中查找元素
在受污染的二叉树中查找元素
LeetCode-1261
/**
* 给出一个满足下述规则的二叉树:
* root.val == 0
* 如果 treeNode.val == x 且 treeNode.left != null,那么 treeNode.left.val == 2 * x + 1
* 如果 treeNode.val == x 且 treeNode.right != null,那么 treeNode.right.val == 2 * x + 2
* 现在这个二叉树受到「污染」,所有的 treeNode.val 都变成了 -1。
* 请你先还原二叉树,然后实现 FindElements 类:
* FindElements(TreeNode* root) 用受污染的二叉树初始化对象,你需要先把它还原。
* bool find(int target) 判断目标值 target 是否存在于还原后的二叉树中并返回结果。
**/
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
#include<map>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/**
0
/ \
1 2
/ \
3 4
**/
class FindElements {
private:
map<int,int>ma;
void DFS(TreeNode* root){
if(root->left){
root->left->val=2*(root->val)+1;
ma[root->left->val]++;
DFS(root->left);
}
if(root->right){
root->right->val=2*(root->val)+2;
ma[root->right->val]++;
DFS(root->right);
}
}
public:
FindElements(TreeNode* root) {
root->val=0;
ma[0]++;
DFS(root);
}
bool find(int target) {
return ma[target];
}
};
int main(){
TreeNode* t1=new TreeNode(-1);
TreeNode* t2=new TreeNode(-1);
TreeNode* t3=new TreeNode(-1);
TreeNode* t4=new TreeNode(-1);
TreeNode* t5=new TreeNode(-1);
t2->left=t4;t2->right=t5;
t1->left=t2;t1->right=t3;
FindElements* obj = new FindElements(t1);
cout<<obj->find(3)<<endl;
system("pause");
return 0;
}
LeetCode-在受污染的二叉树中查找元素的更多相关文章
- LeetCode 5264 在受污染的二叉树中查找元素 Find Elements in a Contaminated Binary Tree
地址 https://leetcode-cn.com/contest/weekly-contest-163/problems/find-elements-in-a-contaminated-binar ...
- leetcode1261在受污染的二叉树中查找元素
题目 一颗二叉树,树根值为0,父节点为x,则左子值为2x+1,右子为2x+2.现在只有树的结构,所有值都变为-1被污染了.求污染前是否存在某个值. 构建一次树,查询会调用多次. 题解 这道题还是比较简 ...
- leetcode-163周赛-1261-在污染的二叉树中查找元素
题目描述: 方法一: class FindElements: def __init__(self, root: TreeNode): self.d = set() def f(r, x): if r: ...
- 【LeetCode】34. 在排序数组中查找元素的第一个和最后一个位置
34. 在排序数组中查找元素的第一个和最后一个位置 知识点:数组,二分查找: 题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置 ...
- Leetcode题目34.在排序数组中查找元素的第一个和最后一个位置(中等)
题目描述: 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标 ...
- 【LeetCode 34】在排序数组中查找元素的第一个和最后一个位置
题目链接 [题解] 二分某个数的上下界. 其实这个方法并不难. 只要你想清楚了二分最后一次执行的位置在什么地方就不难了. [代码] class Solution { public: vector< ...
- 后序线索二叉树中查找结点*p的后继
在后序线索二叉树中查找结点*p的后继: 1.若结点*p为根,则无后继:2.若结点*p为其双亲的右孩子,则其后继为其双亲:3.若结点*p为其双亲的左孩子,且双亲无右子女,则其后继为其双亲:4.若结点*p ...
- Leetcode算法【34在排序数组中查找元素】
在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...
- Java实现 LeetCode 34 在排序数组中查找元素的第一个和最后一个位置
在排序数组中查找元素的第一个和最后一个位置 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n ...
随机推荐
- Codeforces Round #644 (Div. 3) D. Buying Shovels (数学)
题意:商店里有\(k\)个包裹,第\(i\)个包裹中含有\(i\)个物品,现在想要买\(n\)物品,你可以选择某一个包裹购买任意次,使得物品数刚好等于\(n\),求最少的购买次数. 题解:首先,假如\ ...
- dict与set -- Python
dict(字典):用空间换取时间,占据空间大,但查询速度快,键值对(key:value),key唯一 d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} 由于一个k ...
- LEETCODE - 160【相交链表】
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- μC/OS-III---I笔记2---实钟节拍
所谓时钟节拍,就是CPU以固定的频率产生中断,可以看做是系统的心跳.内核利用这个时钟节拍来管理各个任务的一些时间管理比如延时,定时,超时检测,时间轮片调度等.时钟节拍的频率一般10Hz--1000Hz ...
- 全球最好 css3 website
http://www.awwwards.com/ http://www.revolution.pn/ http://www.bestcss.in/ http://www.csswinner.com/ ...
- free website generator by google
free website generator by google https://sites.google.com/view/webgeeker-xyz/首页 https://sites.google ...
- 如何关闭 iPad Pro 自动开启 wifi 和蓝牙
如何关闭 iPad Pro 自动开启 wifi 和蓝牙 为了省电,明明关闭了,但是发现每天都会自动开启,什么鬼设计 https://support.apple.com/zh-cn/HT208086 h ...
- Android Studio 4.x
Android Studio 4.x https://developer.android.com/studio https://d.android.com/r/studio-ui/whats-new- ...
- taro 如何展示多行文本 省略号
taro 如何展示多行文本 省略号 webkit-box-orient: vertical; See the Pen Pure CSS multiline text with ellipsis by ...
- react fiber
react fiber https://github.com/acdlite/react-fiber-architecture https://github.com/facebook/react/is ...