代码随想录算法训练营Day14||Leecode226.翻转二叉树

一、反转二叉树

        递归法,类似直接遍历。swap的位置就是“中”的位置,inverval(cur->left)就是“左”的位置。因为swap是针对父节点的操作,所以可以吧swap的位置等效替换成“中”的位置。

/**
 * 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:
    void interval(TreeNode*&root){
        TreeNode*cur=root;
        if(root==nullptr)return;
        swap(cur->left,cur->right);
        interval(cur->left);
        interval(cur->right);
    }
    TreeNode* invertTree(TreeNode* root) {
       interval(root);
       return root;
    }
};

二、对称二叉树

 递归传参:左右儿子节点的指针

递归结束条件:指向下一组左右儿子节点的指针是否为空

单层递归逻辑:判断外部节点是否相同,判断内部节点是否相同。

/**
 * 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 compare(TreeNode*left,TreeNode*right){
        if(left==nullptr&&right==nullptr)return true;
        else if(left==nullptr&&right!=nullptr)return false;
        else if(left!=nullptr&&right==nullptr)return false;
        else if(left!=nullptr&&right!=nullptr&&left->val!=right->val)return false;
        else{
            bool outside=compare(left->left,right->right);
            bool inside=compare(left->right,right->left);
            bool result=outside&&inside;
            return result;
        }
    }
    bool isSymmetric(TreeNode* root) {
       if(root==nullptr)return true;
       return compare(root->left,root->right);
    }
};

三、二叉树的最大深度

        记录一下这个令人感动的时刻!!!T.T第一次自己AC了二叉树这里的题,虽然不是用递归,用层序遍历做的

        

/**
 * 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 maxDepth(TreeNode* root) {
        if(root==nullptr)return 0;
        int returnsize=0;
        queue<TreeNode*>que;
        que.push(root);
        while(!que.empty()){
           
            int size=que.size();
            
            for(int i=0;i<size;i++){
                TreeNode*temp=que.front();
                if(temp->left)que.push(temp->left);
                if(temp->right)que.push(temp->right);
                que.pop();
            }
            returnsize++;

        }

        return returnsize;
    }
};

递归方法:

        深度和高度一样都是采用后序遍历,在每一层节点中,先去探索左子树的最大深度,再探索右子树的最大深度,让二者比较取一个更大值+1(+1代表遍历过了一层)

/**
 * 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 maxDepth(TreeNode* root) {
       TreeNode*node=root;
       if(node==nullptr)return 0;
       
      
       int leftdepth=maxDepth(node->left);
       int rightdepth=maxDepth(node->right);
       int depth=1+max(leftdepth,rightdepth);
       return depth;
    }
};

四、最小深度

        这里的特殊处理有点懵,也就是说,求最小深度是遇到了nullptr要给它特殊标记,左侧如果为空,右侧依然需要算。

        递归写法: 后序遍历

/**
 * 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 minDepth(TreeNode* root) {
        int result=0;
        TreeNode*node=root;
        if(node==nullptr)return 0;
        int leftmindepth=minDepth(node->left);
        int rightmindepth=minDepth(node->right);
        if(node->left==nullptr&&node->right!=nullptr){
            return 1+rightmindepth;
        }
        if(node->left!=nullptr&&node->right==nullptr){
            return 1+leftmindepth;
        }
        int minresult=1+min(leftmindepth,rightmindepth);
        return minresult;
    }
};

 

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值