DFS
6. 计算布尔二叉树的值(medium)
https://leetcode.cn/problems/evaluate-boolean-binary-tree/submissions/577469929/
/**
* 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==1?true:false;
bool left=evaluateTree(root->left);
bool right=evaluateTree(root->right);
return root->val==2?left||right:left&&right;
}
};
7. 求根节点到叶节点数字之和(medium)
https://leetcode.cn/problems/sum-root-to-leaf-numbers/submissions/577475267/
/**
* 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 _sumNumbers(root,0);
}
int _sumNumbers(TreeNode* root,int Presum)
{
Presum=Presum*10+root->val;
if(root->left==nullptr&&root->right==nullptr)
return Presum;
int ret=0;
if(root->left)
ret+=_sumNumbers(root->left,Presum);
if(root->right)
ret+=_sumNumbers(root->right,Presum);
return ret;
}
};
8. 二叉树剪枝(medium)
https://leetcode.cn/problems/binary-tree-pruning/submissions/577479516/
/**
* 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)
root=nullptr;
return root;
}
};
9. 验证二叉搜索树(medium)
https://leetcode.cn/problems/validate-binary-search-tree/submissions/577484480/
/**
* 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 {
long prev=LONG_MIN;
public:
bool isValidBST(TreeNode* root) {
if(root==nullptr)
return true;
bool left=isValidBST(root->left);
bool cur=false;
if(root->val>prev)
{
cur=true;
prev=root->val;
}
bool right=isValidBST(root->right);
return left&&right&&cur;
}
};
10. 二叉搜索树中第k小的元素(medium)
https://leetcode.cn/problems/kth-smallest-element-in-a-bst/submissions/577486024/
/**
* 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 {
int count;
int ret;
public:
int kthSmallest(TreeNode* root, int k) {
count=k;
_kthSmallest(root);
return ret;
}
void _kthSmallest(TreeNode* root)
{
if(root==nullptr)return;
_kthSmallest(root->left);
count--;
if(count==0)
ret=root->val;
_kthSmallest(root->right);
}
};