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

/**

* 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 vis[7];

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(!vis[i]) {

path.push_back(nums[i]);

vis[i]=true;

dfs(nums);

path.pop_back();

vis[i]=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(nums[pos]);

dfs(nums,pos+1);

path.pop_back();

dfs(nums,pos+1);

}

};

相关推荐
️是789 分钟前
信息奥赛一本通—编程启蒙(3395:练68.3 车牌问题)
数据结构·c++·算法
Liangwei Lin25 分钟前
LeetCode 118. 杨辉三角
算法·leetcode·职场和发展
计算机安禾27 分钟前
【c++面向对象编程】第24篇:类型转换运算符:自定义隐式转换与explicit
java·c++·算法
鼠鼠我(‘-ωก̀ )好困29 分钟前
leetGPU
算法
我星期八休息40 分钟前
Linux系统编程—基础IO
linux·运维·服务器·c语言·c++·人工智能·算法
池塘的蜗牛1 小时前
A Low-Complexity Method for FFT-based OFDM Sensing
算法
故事和你911 小时前
洛谷-【图论2-1】树5
开发语言·数据结构·c++·算法·动态规划·图论
咖啡里的茶i2 小时前
视觉显著目标的自适应分割与动态网格生成算法研究
人工智能·算法·目标跟踪
paeamecium2 小时前
【PAT甲级真题】- String Subtraction (20)
数据结构·c++·算法·pat考试·pat
YL200404262 小时前
047从前序与中序遍历序列构造二叉树
算法·leetcode