• 专题二:二叉树的深搜【递归、搜索、回溯】


    深度优先遍历DFS,全称为DepthFirstTraversal),是我们树或者图这样的数据结构中常用的⼀种遍历算法。这个算法会尽可能深的搜索树或者图的分⽀,直到⼀条路径上的所有节点都被遍历完毕,然后再回溯到上⼀层,继续找⼀条路遍历。
    在⼆叉树中,常⻅的深度优先遍历为:前序遍历、中序遍历以及后序遍历。
     三种遍历方式的最大区别是:处理根节点的时机不同。 

    1、计算布尔二叉树的值

    1. //后序遍历
    2. class Solution {
    3. public:
    4. bool evaluateTree(TreeNode* root) {
    5. if(root->left == nullptr) return root->val;
    6. bool l = evaluateTree(root->left);
    7. bool r = evaluateTree(root->right);
    8. if(root->val == 2) return l | r;
    9. else return l & r;
    10. }
    11. };

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

    1. class Solution {
    2. public:
    3. int dfs(TreeNode* root,int presum)
    4. {
    5. presum = presum*10+root->val;
    6. if(root->left == nullptr && root->right == nullptr) return presum;
    7. int ret = 0;
    8. if(root->left) ret += dfs(root->left,presum);
    9. if(root->right) ret += dfs(root->right,presum);
    10. return ret;
    11. }
    12. int sumNumbers(TreeNode* root) {
    13. return dfs(root,0);
    14. }
    15. };

    3、二叉树剪枝

             

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. TreeNode* pruneTree(TreeNode* root) {
    15. if(root==nullptr) return nullptr;
    16. root->left = pruneTree(root->left);
    17. root->right = pruneTree(root->right);
    18. if(root->left == nullptr && root->right == nullptr && root->val == 0)
    19. {
    20. delete root;
    21. root = nullptr;
    22. }
    23. return root;
    24. }
    25. };

    4、验证二叉搜索树

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. long prev = LONG_MIN;
    15. bool isValidBST(TreeNode* root) {
    16. if(root == nullptr) return true;
    17. bool left = isValidBST(root->left);
    18. if(left == false) return false;
    19. bool cur = false;
    20. if(root->val > prev)
    21. {
    22. cur = true;
    23. }
    24. if(cur == false) return false;
    25. prev = root->val;
    26. bool right = isValidBST(root->right);
    27. return left && right && cur;
    28. }
    29. };

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

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. int count;
    15. int ret;
    16. int kthSmallest(TreeNode* root, int k) {
    17. count = k;
    18. dfs(root);
    19. return ret;
    20. }
    21. void dfs(TreeNode* root)
    22. {
    23. if(root == nullptr || count == 0)
    24. return;
    25. dfs(root->left);
    26. count--;
    27. if(count == 0) ret = root->val;
    28. dfs(root->right);
    29. }
    30. };

    6、二叉树的所有路径

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. vector ret;
    15. vector binaryTreePaths(TreeNode* root) {
    16. string path;
    17. dfs(root,path);
    18. return ret;
    19. }
    20. void dfs(TreeNode* root,string path)
    21. {
    22. path += to_string(root->val);
    23. if(root->left == nullptr && root->right == nullptr)
    24. {
    25. ret.push_back(path);
    26. return;
    27. }
    28. path += "->";
    29. if(root->left) dfs(root->left,path);
    30. if(root->right) dfs(root->right,path);
    31. }
    32. };

  • 相关阅读:
    技术管理进阶——为什么Leader的话有时候你听不懂
    【2022】Nginx使用相应模块做出访问限制
    PCA 在图像分析上的应用
    黑帽python第二版(Black Hat Python 2nd Edition)读书笔记 之 第二章 网络工程基础(2)创建一个TCP代理
    Canal整合SpringBoot详解(二)
    由置灰引出的css滤镜filter是什么东西?
    leetcode第311场周赛题解
    【AI】深度学习——循环神经网络
    第一章 visual studio下载安装
    7.堆叠注入
  • 原文地址:https://blog.csdn.net/m0_73065213/article/details/133636657