• 二叉树与递归问题


    目录

    一:求二叉树的深度

     二:二叉树反转

    三:二叉树镜像判断 

    四:递归的终止条件


    用递归解决的问题必须注意的

    • 递归的终止条件,也就是递归的出口(否则:栈溢出)
    • 递归的过程,也就是最小子结构---逻辑

    二叉树是天然的递归结构-----结构就是按照递归定义的

    一:求二叉树的深度


    1. class Solution {
    2. public:
    3. int maxDepth(TreeNode* root) {
    4. int res=0;//定义返回结果
    5. /*递归终止条件*/
    6. if(root==NULL) return res;
    7. /*递归过程--求出左子树高度,求出右子树高度,得出结果*/
    8. int left=maxDepth(root->left);
    9. int right=maxDepth(root->right);
    10. res=max(left,right)+1;//自己占一层
    11. return res;
    12. }
    13. };

    可以不申请变量

    1. int maxDepth(TreeNode* root) {
    2. int res=0;//定义返回结果
    3. /*递归终止条件*/
    4. if(root==NULL) return res;
    5. /*递归过程--求出左子树高度,求出右子树高度,得出结果*/
    6. res=max(maxDepth(root->left),maxDepth(root->right))+1;//自己占一层
    7. return res;
    8. }

     二:二叉树反转

    1. TreeNode* invertTree(TreeNode* root) {
    2. //递归结束条件
    3. if(root==NULL) return root;
    4. //递归:左子树和右子树互换
    5. TreeNode* tmp=invertTree(root->right);
    6. root->right=invertTree(root->left);
    7. root->left=tmp;
    8. return root;
    9. }

    这个思路很清晰:

    返回值一定是根节点------为NULL返回,不为空执行递归过程。

    在写递归逻辑时,一定从最简单入手。

    因为左子树和右子树要交换,定义变量tmp接收右子树的结果,然后把右子树直接传入左子树,最后交换即可。

    1. TreeNode* invertTree(TreeNode* root) {
    2. //递归结束条件
    3. if(root==NULL) return root;
    4. //递归:左子树和右子树互换
    5. invertTree(root->left);
    6. invertTree(root->right);
    7. swap(root->left,root->right);
    8. return root;
    9. }

    这里不格外定义变量

    三:二叉树镜像判断 

    给出的函数------bool isSymmetric(TreeNode* root)

    思路分析:

    这里函数只给出一个变量,但是因为轴的存在,肯定的需要两个指针分别指向一左一右来比较数值

    bool twoSymNodes(TreeNode* p,TreeNode* q)

    然后只要判断,首先一个为空另一个不为空一定不对称

    其次:都不为空时,如果数值不同,一定对称。

    最后递归:结束递归(两个指针为空),接受

    递归:不断传入p左q右和p右q左

    1. /*
    2. 说白了就是:比较同层次的两个结点p,q
    3. 保证:p左=q右;p右=q左
    4. */
    5. class Solution {
    6. public:
    7. bool twoSymNodes(TreeNode* p,TreeNode* q){
    8. //只考虑存在
    9. if(p&&!q) return false;
    10. else if(!p&&q) return false;
    11. else if(!p&&!q) return true;
    12. else{
    13. if(p->val!=q->val) return false;
    14. return twoSymNodes(p->left,q->right)&&twoSymNodes(p->right,q->left);
    15. }
    16. }
    17. bool isSymmetric(TreeNode* root) {
    18. //空树对称
    19. if(root==NULL) return true;
    20. //比较---最复杂情况(p左=q右 + p右=q左)
    21. return twoSymNodes(root,root);
    22. }
    23. };
    1. class Solution {
    2. public:
    3. bool twoSymNodes(TreeNode* p,TreeNode* q){
    4. //递归结束的接受状态
    5. if(!p&&!q) return true;//p,q均为空
    6. //不接受状态
    7. if((p&&!q)||(!p&&q)) return false;
    8. assert(p&&q);
    9. if(p->val!=q->val) return false;
    10. else{
    11. //递归
    12. return twoSymNodes(p->left,q->right)&&twoSymNodes(p->right,q->left);
    13. }
    14. }
    15. bool isSymmetric(TreeNode* root) {
    16. return twoSymNodes(root,root);
    17. }
    18. };

    当然可以优化一些细节,比如主程序的root==NULL可以不判断。

    这里:传参数量不能满足题目要求,要自己设计函数传参

    四:递归的终止条件


    分析:

    算法思想就是:

    在左右子树寻找sum-当前结点值,如果相等了只要该结点是叶子结点就满足题意 

    1. bool hasPathSum(TreeNode* root, int targetSum) {
    2. if(root==NULL) return false;//空树无路径
    3. if(!root->left&&!root->right&&root->val==targetSum) return true;
    4. bool a=hasPathSum(root->left,targetSum-(root->val));
    5. bool b=hasPathSum(root->right,targetSum-(root->val));
    6. if(a||b) return true;
    7. else return false;
    8. }

    注意递归终止条件和递归的使用。

    1. bool hasPathSum(TreeNode* root, int targetSum) {
    2. if(root==NULL) return false;
    3. if(!root->left&&!root->right) return root->val==targetSum;
    4. if(hasPathSum(root->left,targetSum-(root->val))) return true;
    5. if(hasPathSum(root->right,targetSum-(root->val))) return true;
    6. return false;
    7. }

     这个写法也满足:叶子结点等于符合条件的值

  • 相关阅读:
    DDR电源硬件设计要点
    第九章 聚类
    计算机网络 | 刷题笔记
    Idea常用插件及其作用
    Java项目硅谷课堂学习笔记-P8点播模块管理-后台-管理员端
    tokenizer.tokenize(), tokenizer.encode() , tokenizer.encode_plus() 方法介绍及其区别
    MobileNet系列(4):MobileNetv3网络详解
    学期伊始,来填两年前的flag,这是我的笔记博客网站!
    数据标注典型案例,景联文科技如何助力企业搭建数据方案
    如何通过低代码平台来实现移动办公
  • 原文地址:https://blog.csdn.net/weixin_47173597/article/details/126920277