怎么攻击php做的网站吗/百度关键词搜索工具
在受污染的二叉树中查找元素
方法一
先用dfs自上而下地修改值。
然后查找的某个的值时候,序列是确定的。比如,9->4->1->0。
然后用迭代的方式去查询这个序列。
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class FindElements {
public:TreeNode* root;FindElements(TreeNode* root) {this->root = root;root->val = 0;dfs(root);}void dfs(TreeNode* root){if(!root){return;}if(root->left){root->left->val = 2*root->val+1;dfs(root->left);}if(root->right){root->right->val = 2*root->val+2;dfs(root->right);}}bool find(int t) {stack<int> qs;while(t>0){qs.push(t);t = (t-1)/2;}TreeNode* p = root;while(!qs.empty()){if(!p){return false;}int x = qs.top();qs.pop();if(x==p->val*2+1){if(p->left)p = p->left;elsereturn false;}else if(x==p->val*2+2){if(p->right)p = p->right;elsereturn false;}else{return false;}}return true; }
};/*** Your FindElements object will be instantiated and called as such:* FindElements* obj = new FindElements(root);* bool param_1 = obj->find(target);*/
方法二
利用层序遍历的有序,然后直接用binary_search()
二分查找。
class FindElements {
public:vector<int> list;FindElements(TreeNode* root) {root->val = 0;dfs(root);layerOrder(root);}void dfs(TreeNode* root){if(!root){return;}if(root->left){root->left->val = 2*root->val+1;dfs(root->left);}if(root->right){root->right->val = 2*root->val+2;dfs(root->right);}}void layerOrder(TreeNode* root){queue<TreeNode*> q;q.push(root);while(q.size()){TreeNode* p = q.front();q.pop();list.push_back(p->val);if(p->left){q.push(p->left);}if(p->right){q.push(p->right);}}}bool find(int target) {return binary_search(list.begin(),list.end(),target);}
};