【C++刷题】二叉树的深搜


一、计算布尔二叉树的值

1、题目描述

leetcode链接

在这里插入图片描述

2、代码

class Solution 
{
public:
    bool evaluateTree(TreeNode* root) 
    {
        // 出口
        if(root->left == nullptr)
        {
            if(root->val == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        // 左子树
        bool Left = evaluateTree(root->left);
        bool Right = evaluateTree(root->right);

        // 结点往上返回
        if(root->val == 2)
        {
            return Left | Right;
        }
        else
        {
            return Left & Right;
        }
    }
};

3、解析

在这里插入图片描述

二、求根节点到叶节点数字之和

1、题目描述

leetcode链接
在这里插入图片描述

2、代码

class Solution 
{
public:
    int sumNumbers(TreeNode* root) 
    {
        return dfs(root, 0);
    }
    int dfs(TreeNode* root, int persum)
    {
        persum = persum * 10 + root->val;
        // 1、出口
        if(root->left == nullptr && root->right == nullptr)
        {
            return persum;
        }
        // 2、函数体
        int ret = 0;
        if(root->left)
        {
            ret += dfs(root->left, persum);
        }
        if(root->right)
        {
            ret += dfs(root->right, persum);
        }
        
        return ret;
    }
};

3、解析

在这里插入图片描述

三、二叉树剪枝

1、题目描述

leetcode链接
在这里插入图片描述

2、代码

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;
    }
};

3、解析

在这里插入图片描述

四、验证二叉搜索树

1、题目描述

leetcode链接
在这里插入图片描述

2、代码

class Solution 
{
public:
    // 定义一个prev值为最小
    long prev = LONG_MIN;
    bool isValidBST(TreeNode* root) 
    {
        // 出口
        if(root == nullptr)
        {
            return true;
        }
        bool Lefttree = isValidBST(root->left);
        // 剪枝
        if(Lefttree == false)
            return false;
        bool cur = false; // 判断当前是不是二叉搜索树
        if(root->val > prev)
        {
            cur = true;
        }
        // 剪枝
        if(cur == false)
        {
            return false;
        }
        prev = root->val; // 更新
        bool Righttree = isValidBST(root->right);
        if(Lefttree == true && Righttree == true && cur == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};

3、解析

在这里插入图片描述

五、二叉搜索树中第K小的元素

1、题目描述

leetcode链接
在这里插入图片描述

2、代码

class Solution 
{
public:
    int ret = 0;
    int count = 0;
    int kthSmallest(TreeNode* root, int k) 
    {
        count = k;
        dfs(root);
        return ret;
    }
    void dfs(TreeNode* root)
    {
        // 1、出口
        if(root == nullptr || count == 0)
        {
            return;
        }
        // 2、中序遍历
        dfs(root->left);
        count--;
        if(count == 0)
        {
            // 3、优化:剪枝
            ret = root->val;
            return;
        }
        dfs(root->right);
    }
};

3、解析

在这里插入图片描述

六、二叉树的所有路径

1、题目描述

leetcode链接
在这里插入图片描述

2、代码

// 不加剪枝
class Solution 
{
public:
    // 1、定义一个全局返回变量ret
    vector<string> ret;
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        // 2、进行dfs函数的调用
        string path;
        dfs(root, path);
        return ret;
    }
    void dfs(TreeNode* root, string path)
    {
        // 3、递归出口
        if(root == nullptr) return;
        // 4、叶子结点的增加路径
        if(root->right == nullptr && root->left == nullptr)
        {
            path += to_string(root->val);
            ret.push_back(path);
        }
        // 5、非叶子节点
        path += to_string(root->val);
        path += "->";
        // 6、递归
        dfs(root->left, path);
        dfs(root->right, path);
    }
};
// 加剪枝
class Solution 
{
public:
    vector<string> ret;
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        string path;
        dfs(root, path);
        return ret;
    }
    void dfs(TreeNode* root, string path)
    {
        if(root->left == nullptr && root->right == nullptr)
        {
            path += to_string(root->val);
            ret.push_back(path);
            return;
        }
        path += to_string(root->val);
        path += "->";
        if(root->left) dfs(root->left, path);
        if(root->right) dfs(root->right, path);
    }
};

3、解析

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2022horse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值