代码随想录跟练日记|Day14 二叉树part02

算法小白训练营日记,笔记为自用,若有错误感谢指出

--今日任务--:226.翻转二叉树 ,101. 对称二叉树 ,104.二叉树的最大深度 ,111.二叉树的最小深度

226.翻转二叉树

题目链接:226. 翻转二叉树 - 力扣(LeetCode)

解题思路:

递归法:先交换左右子树位置,再遍历左右子树(前序),也可以先遍历子树再交换(后序)。具体代码如下:

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==NULL) return root;
        swap(root->left,root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

迭代法:前序遍历二叉树,遍历到一个节点时交换左右子树位置,再一次将右子树和左子树节点加入栈中。具体代码如下:

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==NULL) return root;
        stack<TreeNode*> st;
        st.push(root);
        while(!st.empty()){
            TreeNode* node=st.top();
            st.pop();
            swap(node->left,node->right);
            if(node->left) st.push(node->left);
            if(node->right) st.push(node->right);
        }
        return root;
    }
};

101. 对称二叉树

题目链接:101. 对称二叉树 - 力扣(LeetCode)

解题思路:

递归法:(后序)每次比较节点的左右子树,从下至上依次返回比较结果。具体代码如下:

class Solution {
public:
    bool compare(TreeNode*left,TreeNode*right){
        if(left==NULL&&right==NULL) return true;
        else if(left==NULL&&right!=NULL) return false;
        else if(left!=NULL&&right==NULL) return false;
        else if(left->val!=right->val) return false;
        
        bool outside=compare(left->left,right->right);
        bool inside=compare(left->right,right->left);
        bool isSame=outside&&inside;
        return isSame;
    }

    bool isSymmetric(TreeNode* root) {
        if(root==NULL) return true;
        return compare(root->left,root->right);
    }
};

迭代法:运用队列方式,添加节点到队列里时,依次添加相对称的节点,每次比较队列的前两个节点即互为对称的节点是否相等,若相等则继续比较该节点的下一层节点。

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root==NULL) return true;
        queue<TreeNode*> que;
        que.push(root->left);
        que.push(root->right);
        while(!que.empty()){
            TreeNode* leftNode=que.front();que.pop();
            TreeNode* rightNode=que.front();que.pop();
            if(!leftNode&&!rightNode) continue;
            if(!leftNode||!rightNode||(leftNode->val!=rightNode->val)) return false;
            que.push(leftNode->left);
            que.push(rightNode->right);
            que.push(leftNode->right);
            que.push(rightNode->left);
        }
        return true;
    }
};

104.二叉树的最大深度

题目链接:104. 二叉树的最大深度 - 力扣(LeetCode)

解题思路:递归法:二叉树根结点的高度即二叉树的深度,后序遍历二叉树,从下往上依次返回每一层的高度,直到返回到根节点。每层的高度是该节点左右子树高度最大值+1,(1是该节点本身的高度)。具体代码如下:

class Solution {
public:
    int getDepth(TreeNode* node){
        if(node==NULL) return 0;
        int leftdepth=getDepth(node->left);
        int rightdepth=getDepth(node->right);
        int depth=1+max(leftdepth,rightdepth);
        return depth;
    }

    int maxDepth(TreeNode* root) {
        return getDepth(root);
    }
};

111.二叉树的最小深度

题目链接:111. 二叉树的最小深度 - 力扣(LeetCode)

解题思路:和上道题类似,区别是求到叶子结点的最小距离,所以判断条件中要加上若左子树为空则取右子树的值,右子树为空则取左子树的值,否则取左右子树最小值。具体代码如下:

class Solution {
public:
    int getDepth(TreeNode* node){
        if(node==NULL) return 0;
        int leftdepth=getDepth(node->left);
        int rightdepth=getDepth(node->right);
        if(node->left!=NULL&&node->right==NULL) return 1+leftdepth;
        else if(node->left==NULL&&node->right!=NULL) return 1+rightdepth;
        else return 1+min(leftdepth,rightdepth);
    }

    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值