回溯算法专题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);
    }
};
相关推荐
3DVisionary20 小时前
aero-engine-blade-thermal-fatigue-dic-inspection
人工智能·算法·机器学习·航空发动机·高温dic·涡轮叶片·热疲劳
asdfg125896320 小时前
一文理解Java中的泛型
java·开发语言
飞翔中文网20 小时前
Java学习笔记之反射
java·笔记·学习
Kurisu57520 小时前
深度拆解:从二进制切片到并发控制,大文件断点续传的底层工程设计
算法
Hiter_John20 小时前
Golang的变量常量初始化
开发语言·后端·golang
梦想的颜色20 小时前
MySQL 数据存储结构与查询执行生命周期深度解析
运维·数据结构·数据库·mysql·线程·优化
电商API_1800790524720 小时前
免 TOP 入驻,第三方淘宝商品详情 API 快速接入与代码示例
java·大数据·开发语言·数据库·爬虫·数据分析
Ameilide20 小时前
数据结构 算法解释,排序、查找
数据结构
c2385620 小时前
C++列表初始化与变量类型推导
开发语言·c++
代码小库20 小时前
【2026前端最新面试题——day10】JavaScript 高频面试题
开发语言·前端·javascript