回溯算法专题2:二叉树中的深搜

二叉树中的深搜

目录

二叉树中的深搜

试题1:计算布尔二叉树的值

算法原理

代码编写

试题2:求根节点到叶节点数字之和

算法原理

代码编写

试题3:二叉树剪枝

算法原理

代码编写

试题4:验证二叉搜索树

算法原理

代码编写

试题5:二叉搜索树中第k小的元素

算法原理

代码编写

试题6:二叉树的所有路径

算法原理

代码编写


试题1:计算布尔二叉树的值

算法原理

解法:递归

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    bool evaluateTree(TreeNode* root) 
    {
        return dfs(root);
    }
    bool dfs(TreeNode* root)
    {
        if(root == nullptr)
        {
            return false;
        }
        if(root->left == nullptr && root->right == nullptr)
        {
            return root->val;
        }
        bool left = dfs(root->left);
        bool right = dfs(root->right);
        if(root->val == 2)
        {
            return right | left;
        }
        else
        {
            return right & left;
        }
    }
};
  • 标准版本
cpp 复制代码
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;
    }
};

试题2:求根节点到叶节点数字之和

算法原理

解法:递归

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    int sumNumbers(TreeNode* root) 
    {
        int ret = dfs(root, 0);
        return ret;
    }
    int dfs(TreeNode* root, int prenum)
    {
        if(root == nullptr)
        {
            return 0;
        }
        int num = prenum * 10 + root->val;
        if(root->left == nullptr && root->right == nullptr)
        {
            return num;
        }
        int left = dfs(root->left, num);
        int right = dfs(root->right, num);
        return left + right;
    }
};
  • 标准版本
cpp 复制代码
class Solution 
{
public:
    int sumNumbers(TreeNode* root) 
    {
        return dfs(root, 0);
    }
    int dfs(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 += dfs(root->left, presum);
        if(root->right) ret += dfs(root->right, presum);
        return ret;
    }
};

试题3:二叉树剪枝

算法原理

解法:递归

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    TreeNode* pruneTree(TreeNode* root) 
    {
        return dfs(root);
    }
    TreeNode* dfs(TreeNode* root)
    {
        if(root == nullptr)
        {
            return root;
        }
        root->left = dfs(root->left);
        root->right = dfs(root->right);
        if(root->val == 0 && root->right == nullptr && root->left == nullptr)
        {
            root = nullptr;
        }
        return root;
    }
};
  • 标准版本
cpp 复制代码
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;
    }
};

试题4:验证二叉搜索树

算法原理

解法:递归

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
public:
    long long prev = -LLONG_MAX;
    bool isValidBST(TreeNode* root) 
    {
        if(root == nullptr)
        {
            return true;
        }
        if(!isValidBST(root->left)) 
        {
            return false;
        }
        if(root->val <= prev)
        {
            return false;
        }
        prev = root->val;
        if(!isValidBST(root->right))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
};
  • 标准版本
cpp 复制代码
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;
    }
};
  • 优化版本
cpp 复制代码
class Solution
{
    long prev = LONG_MIN;
public:
    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;
    }
};

试题5:二叉搜索树中第k小的元素

算法原理

解法:递归

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
    int count;
    int ret;
public:
    int kthSmallest(TreeNode* root, int k) 
    {
        count = k;
        ret = 0;
        dfs(root);
        return ret;
    }
    void dfs(TreeNode* root)
    {
        if(root == nullptr)
        {
            return;
        }
        dfs(root->left);
        count--;
        if(count == 0)
        {
            ret = root->val;
            return;
        }
        dfs(root->right);
    }
};
  • 标准版本
cpp 复制代码
class Solution 
{
    int count;
    int ret;
public:
    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);
    }
};

试题6:二叉树的所有路径

算法原理

解法:递归

代码编写

  • 个人版本
cpp 复制代码
class Solution 
{
    vector<string> ret;
public:
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        string path;
        dfs(root, path);
        return ret;
    }
    void dfs(TreeNode* root, string path)
    {
        if(root == nullptr)
        {
            return;
        }
        if(root->left == nullptr && root->right == nullptr)
        {
            path += to_string(root->val);
            ret.push_back(path);
        }
        else
        {
            path += to_string(root->val) + "->";
        }
        dfs(root->left, path);
        dfs(root->right,path);
    }
};
  • 标准版本
cpp 复制代码
class Solution 
{
    vector<string> ret;
public:
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        string path;
        if(root == nullptr) return ret;
        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);
            return;
        }
        path += "->";
        //剪枝
        if(root->left) dfs(root->left, path);//回溯
        if(root->right) dfs(root->right,path);
    }
};
相关推荐
jiayong231 小时前
第 43 课:任务详情抽屉里的批量处理闭环与删除联动
java·开发语言·前端
likerhood1 小时前
Java 访问修饰符:public、protected、private讲解
java·开发语言·javascript
刀法如飞1 小时前
JavaScript 数组去重的 20 种实现方式,学会用不同思路解决问题
前端·javascript·算法
洛水水1 小时前
【力扣100题】46.单词拆分
算法·leetcode·职场和发展
学不思则罔1 小时前
ParallelStream并发陷阱解析
java·开发语言·windows
认真的小羽❅1 小时前
【Java并发编程】volatile关键字深度解析:从内存语义到实际应用
java·开发语言
jayson.h2 小时前
可视化界面
开发语言·python
奋斗的小乌龟2 小时前
langchain4j笔记-08
java·spring boot·笔记
kgduu2 小时前
python中的魔法方法
开发语言·python