代码拉取完成,页面将自动刷新
// 求根结点到叶结点数字之和
https://leetcode.cn/problems/sum-root-to-leaf-numbers/
/**
* 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 ret)
{
if(root == nullptr)
return 0;
if(root->left == nullptr && root->right == nullptr)
return ret*10+root->val;
ret = ret*10 + root->val;
int left = dfs(root->left, ret);
int right = dfs(root->right, ret);
return left+right;
}
};
---------------------------------------------------------------------------------------------------
// 二叉树剪枝
https://leetcode.cn/problems/binary-tree-pruning/
/**
* 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) {
return dfs(root);
}
TreeNode* dfs(TreeNode* root)
{
if(root == nullptr)
return nullptr;
root->left = dfs(root->left);
root->right = dfs(root->right);
if(root->val == 0 && root->left == root->right && root->left == nullptr)
{
delete root;
return nullptr;
}
return root;
}
};
---------------------------------------------------------------------------------------------------
https://leetcode.cn/problems/validate-binary-search-tree/
/**
* 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 isValidBST(TreeNode* root) {
// return dfs(root);
// }
// bool dfs(TreeNode* root)
// {
// if(root == nullptr)
// return true;
// bool ret = dfs(root->left) && dfs(root->right);
// if(root->left == root->right && root->left== nullptr)
// return true;
// if(root->left == nullptr && root->right != nullptr && root->right->val > root->val)
// return true;
// else
// return false;
// if(root->right == nullptr && root->left != nullptr && root->left->val < root->val)
// return true;
// else
// return false;
// if(root->left->val < root->val && root->right->val > root->val)
// return true;
// else
// return false;
// return ret;
// }
// };
// 法二:利用二叉搜索树的一个性质:走中序遍历是有序的。
class Solution {
public:
long long prev = LONG_MIN;
bool isValidBST(TreeNode* root) {
return dfs(root);
}
bool dfs(TreeNode* root)
{
if(root == nullptr)
return true;
bool left = dfs(root->left);
if(root->val <= prev)
return false;
prev = root->val;
bool right = dfs(root->right);
return left && right;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。