【递归、回溯和剪枝】二叉树中的深搜

⼆叉树中的深搜
深度优先遍历(DFS,全称为 Depth First Traversal) ,是我们树或者图这样的数据结构中常⽤的⼀种遍历算法。这个算法会尽可能深的搜索树或者图的分⽀,直到⼀条路径上的所有节点都被遍历完毕,然后再回溯到上⼀层,继续找⼀条路遍历。

在⼆叉树中,常⻅的深度优先遍历为:前序遍历、中序遍历以及后序遍历。

因为树的定义本⾝就是递归定义,因此采⽤递归的⽅法去实现树的三种遍历不仅容易理解⽽且代码很简洁。并且前中后序三种遍历的唯⼀区别就是访问根节点的时机不同,在做题的时候,选择⼀个适当的遍历顺序,对于算法的理解是⾮常有帮助的。

1.计算布尔二叉树的值

计算布尔二叉树的值

思路:

cpp 复制代码
/**
 * 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 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 复制代码
/**
 * 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 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 复制代码
/**
 * 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) {
        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 复制代码
/**
 * 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:
    long prev = LONG_MIN;
    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;
        }
        prev = root->val;
        if(cur == false) return false; // 剪枝
        bool right = isValidBST(root->right);
        return left && right && cur;
    }
};

5.二叉搜索树中第k小的元素

二叉搜索树中第k小的元素

思路:

注意全局变量的妙用

cpp 复制代码
/**
 * 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 count;
    int ret;
    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.二叉树的所有路径

二叉树的所有路径

思路:

path的妙用,把path放在参数列表,可以方便回溯,不需要我们手动进行回溯了。

cpp 复制代码
/**
 * 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:
    vector<string> ret;
    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);
    }
};
相关推荐
水水沝淼㵘几秒前
嵌入式开发学习日志(数据结构--顺序结构单链表)Day19
linux·服务器·c语言·数据结构·学习·算法·排序算法
June`35 分钟前
专题四:综合练习( 找出所有子集的异或总和再求和)
c++·算法·深度优先·剪枝
Magnum Lehar1 小时前
3d游戏引擎的Utilities模块实现下
c++·算法·游戏引擎
JANYI20181 小时前
C语言易混淆知识点详解
java·c语言·算法
绒绒毛毛雨2 小时前
广告推荐算法入门 day1 --项目选型
算法·推荐算法
越城2 小时前
数据结构中的栈与队列:原理、实现与应用
c语言·数据结构·算法
wang__123002 小时前
力扣2094题解
算法·leetcode·职场和发展
GUIQU.3 小时前
【每日一题 | 2025年5.5 ~ 5.11】搜索相关题
算法·每日一题·坚持
不知名小菜鸡.3 小时前
记录算法笔记(2025.5.13)二叉树的最大深度
笔记·算法
真的想上岸啊3 小时前
c语言第一个小游戏:贪吃蛇小游戏05
c语言·算法·链表