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

/**

* 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);

}

};

相关推荐
灵感__idea4 小时前
Hello 算法:“走一步看一步”的智慧
前端·javascript·算法
lwf0061645 小时前
导数学习日记
学习·算法·机器学习
头发够用的程序员6 小时前
从滑动窗口到矩阵运算:img2col算法基本原理
人工智能·算法·yolo·性能优化·矩阵·边缘计算·jetson
武帝为此6 小时前
【数据清洗缺失值处理】
python·算法·数学建模
Halo_tjn7 小时前
Java 基于字符串相关知识点
java·开发语言·算法
念越7 小时前
算法每日一题 Day08|双指针法解决三数之和
算法·力扣
黎阳之光8 小时前
黎阳之光透明管理:视频孪生重构智慧仓储新范式
人工智能·算法·安全·重构·数字孪生
CappuccinoRose9 小时前
回溯法 - 软考备战(四十三)
算法·排列组合·路径·n皇后·子集·解数独·岛屿
AC赳赳老秦9 小时前
OpenClaw进阶技巧:批量修改文件内容、替换关键词,解放双手
java·linux·人工智能·python·算法·测试用例·openclaw
Robot_Nav9 小时前
Shape-Aware MPPI(SA MPPI)算法:基于RC-ESDF的任意形状机器人实时轨迹优化
算法·机器人·sa-mppi