递归在二叉树搜索中的使用

/**

* 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:

bool evaluateTree(TreeNode* root) {

if(root->left==nullptr) return root->val==0?false:true;

bool left=evaluateTree(root->left);

bool right=evaluateTree(root->right);

return root->val==2?left|right:left&right;

}

};

/**

* 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:

int sumNumbers(TreeNode* root) {

return dfs(root,0);

}

int dfs(TreeNode* root,int m)

{

m=m*10+root->val;

if(root->left==nullptr&&root->right==nullptr) return m;

int ret=0;

if(root->left) ret+=dfs(root->left,m);

if(root->right) ret+=dfs(root->right,m);

return ret;

}

};

/**

* 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:

TreeNode* pruneTree(TreeNode* root) {

if(root==nullptr) return nullptr;

root->left=pruneTree(root->left);

root->right=pruneTree(root->right);

if(root->left==nullptr&&root->right==nullptr&&root->val==0){

//delete root;

root=nullptr;

}

return root;

}

};

我们来补充一下tuple和memset的用法:

tuple:

用于三个元素的容器,用get<>(名称)来进行索引

memset;

用于将数据清零

**

* 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:

long prev=LONG_MIN;

bool isValidBST(TreeNode* root) {

if(root==nullptr) return true;

bool left=isValidBST(root->left);

if(left==false) return false;

bool cur=false;

if(root->val>prev) cur=true;

if(cur==false) return false;

prev=root->val;

bool right=isValidBST(root->right);

return left&&right&&cur;

}

};

/**

* 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:

int count;

int ret;

int kthSmallest(TreeNode* root, int k) {

count=k;

dfs(root);

return ret;

}

void dfs(TreeNode* root){

if(root==nullptr||count==0) return;

dfs(root->left);

count--;

if(count==0) ret=root->val;

dfs(root->right);

}

};

/**

* 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<string> ret;

vector<string> binaryTreePaths(TreeNode* root) {

if(root==nullptr) return ret;

string path;

dfs(root,path);

return ret;

}

void dfs(TreeNode* root,string path){

path+=to_string(root->val);

if(root->left==nullptr&&root->right==nullptr){

ret.push_back(path);

}

path+="->";

if(root->left) dfs(root->left,path);

if(root->right) dfs(root->right,path);

}

};

class Solution {

public:

vector<vector<int>> ret;

vector<int> path;

bool vis7;

vector<vector<int>> permute(vector<int>& nums) {

dfs(nums);

return ret;

}

void dfs(vector<int>&nums){

if(path.size()==nums.size()){

ret.push_back(path);

return;

}

for(int i=0;i<nums.size();i++){

if(!visi) {

path.push_back(numsi);

visi=true;

dfs(nums);

path.pop_back();

visi=false;

}

}

}

};

8.

class Solution {

vector<vector<int>> ret;

vector<int> path;

public:

vector<vector<int>> subsets(vector<int>& nums) {

dfs(nums,0);

return ret;

}

void dfs(vector<int>& nums,int pos){

if(pos==nums.size()){

ret.push_back(path);

return;

}

path.push_back(numspos);

dfs(nums,pos+1);

path.pop_back();

dfs(nums,pos+1);

}

};

相关推荐
jidaowansui13 小时前
P11375 [GESP202412 六级] 树上游走
数据结构·算法
hai31524754314 小时前
FlashAttention C语言(C++)实现(展示版)
c语言·开发语言·c++·人工智能·算法
林爷万福15 小时前
光谱数据预处理:基线校正、平滑去噪实战
人工智能·算法
8Qi815 小时前
LeetCode 1049:最后一块石头的重量 II —— 题解 ✅
算法·leetcode·职场和发展·动态规划·01背包
wubba lubba dub dub75015 小时前
第四十九周学习周报
人工智能·算法·机器学习
Java_2017_csdn15 小时前
ComplexKeysShardingAlgorithm 小结
java·大数据·算法
海梨花16 小时前
快手面试高频算法题
java·算法·面试
lqqjuly16 小时前
超分辨率算法深度解析(Super-Resolution Algorithms)
算法
嵌入式老牛17 小时前
液晶段码(米/日字格)识别—倾斜校正
opencv·算法·仿射变换
luj_176817 小时前
残熵算法:风险缓冲与效率优化的融合
c语言·开发语言·网络·经验分享·算法