421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)

226. 翻转二叉树

在这里插入图片描述

1、递归法

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr)    return root;

        swap(root->left, root->right);

        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

2、迭代法

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr) return nullptr;
        stack<TreeNode*> st;
        st.push(root);

        while (!st.empty())
        {
            TreeNode* node = st.top();
            st.pop();

            swap(node->left, node->right);

            if (node->right) st.push(node->right);
            if (node->left) st.push(node->left);
        }
        return root;
    }
};

在这里插入图片描述

101. 对称二叉树

在这里插入图片描述

1、递归法

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

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

2、迭代法

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;
            }

            // 左右一个节点不为空,或者都不为空但数值不相同,返回false
            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.二叉树的最大深度

在这里插入图片描述

1、递归法

class Solution {
public:
    int get_depth(TreeNode* node)
    {
        if (node == nullptr)    return 0;
        int left_depth = get_depth(node->left);
        int right_depth = get_depth(node->right);
        int depth = 1 + std::max(left_depth, right_depth);
        return depth;
    }

public:
    int maxDepth(TreeNode* root) {
        return get_depth(root);
    }
};

2、迭代法

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if (root)    que.push(root);
        int res = 0;

        while (!que.empty())
        {
            int size = que.size();
            res++;

            for (int i = 0; i < size; i++)
            {
                TreeNode* node = que.front();
                que.pop();

                if (node->left)  que.push(node->left);
                if (node->right)  que.push(node->right);
            }
        }
        return res;
    }
};

在这里插入图片描述

222.完全二叉树的节点个数

在这里插入图片描述

1、递归法

class Solution {
public:
    int get_nodes(TreeNode* node)
    {
        if (node == nullptr) return 0;
        int left_nodes = get_nodes(node->left);
        int right_nodes = get_nodes(node->right);
        int nodes = 1 + left_nodes + right_nodes;
        return nodes;
    }

public:
    int countNodes(TreeNode* root) {
        return get_nodes(root);
    }
};

2、迭代法

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if(root)    que.push(root);

        int res = 0;

        while(!que.empty())
        {
            int size = que.size();
            for(int i = 0; i < size; i++)
            {
                res++;
                TreeNode* node = que.front();
                que.pop();

                if(node->left)  que.push(node->left);
                if(node->right) que.push(node->right);
            }
        }
        return res;
    }
};

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liufeng2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值