回溯算法专题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);
    }
};
相关推荐
shehuiyuelaiyuehao31 分钟前
算法68,字符串相乘
算法
Huangjin007_1 小时前
【C++ 算法竞赛篇】STL 常用知识点速通指南
c++·算法
geovindu1 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk81632 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾2 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
驭渊的小故事3 小时前
LeetCode 33:搜索旋转排序数组——从二分查找到旋转数组的区间判定
算法·leetcode·职场和发展
炸膛坦客3 小时前
单片机/C/C++八股:(三十三)取别名
c语言·c++·单片机
听情歌落俗3 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力4 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
ShineWinsu4 小时前
对于Qt:QFontDialog的解析
c++·qt·面试·gui·字体·客户端·qfontdialog