代码随想录第十二天| 226.翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、559. N 叉树的最大深度、111.二叉树的最小深度

    226.翻转二叉树

    思路:交换每个节点的左右孩子即可,使用前序遍历和后序遍历比较好。
    题解:
    递归法前序遍历:

    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    统一迭代法前序遍历

    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            stack<TreeNode*> st;
            if (root == nullptr) return root;
            st.push(root);
            while(!st.empty())
            {
                TreeNode *node = st.top();
                if (node != nullptr)
                {
                    st.pop();
                    if (node->right) st.push(node->right);
                    if (node->left) st.push(node->left);
                    st.push(node);
                    st.push(nullptr);
                }
                else
                {
                    st.pop();
                    node = st.top();
                    swap(node->left, node->right);
                    st.pop();
                }
            }
            return root;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    层序迭代:

    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            queue<TreeNode*> que;
            if (root != nullptr) que.push(root);
            while(!que.empty())
            {
                TreeNode* node = que.front();
                swap(node->left, node->right);
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
            return root;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    101. 对称二叉树

    思路:采用后序遍历,对比左右子树。
    题解:
    递归法后序遍历:

    class Solution {
    public:
        bool cmp(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;
    
            return cmp(left->left, right->right) && cmp(left->right, right->left);
        }
        bool isSymmetric(TreeNode* root) {
            if (root == nullptr) return true;
            return cmp(root->left, root->right);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    迭代法:

    class Solution {
    public:
        bool isSymmetric(TreeNode* root) {
            queue<TreeNode*> que;
            if (root == nullptr) return true;
            que.push(root->left);
            que.push(root->right);
            while(!que.empty())
            {
                TreeNode *l = que.front();
                que.pop();
                TreeNode *r = que.front();
                que.pop();
                if (!l && !r) continue;
                if (!l || !r || l->val != r->val) return false;
                que.push(l->left);
                que.push(r->right);
                que.push(l->right);
                que.push(r->left);
            }
            return true;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    104.二叉树的最大深度

    思路:前序遍历求树的最大深度,后序遍历求根节点的高度(和树的最大深度相等),层序遍历的话是纯模板题。
    题解:
    层序遍历:

    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            queue<TreeNode*> que;
            int res{0};
            if(root != nullptr) que.push(root);
            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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    递归前序遍历:

    class Solution {
    private:
        int result = 0;
    public:
        void getDepth(TreeNode* node, int depth)
        {
            result = depth > result ? depth : result;
            if (node->left == nullptr && node->right == nullptr) return;
            if (node->left)
            {
                ++depth;
                getDepth(node->left, depth);
                --depth;
            }
            if (node->right)
            {
                ++depth;
                getDepth(node->right, depth);
                --depth;
            }
            return;
        }
        int maxDepth(TreeNode* root) {
            if (root == nullptr) return 0;
            getDepth(root, 1);
            return result;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    递归后序遍历:

    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if (root == nullptr) return 0;
            return 1 + max(maxDepth(root->left), maxDepth(root->right));
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    559. N 叉树的最大深度

    思路:和二叉树的最大深度相同。
    题解:
    层序遍历:

    class Solution {
    public:
        int maxDepth(Node* root) {
            queue<Node*> que;
            int res{0};
            if(root != nullptr) que.push(root);
            while(!que.empty())
            {
                int size = que.size();
                ++res;
                for (int i = 0; i < size; ++i)
                {
                    Node *node = que.front();
                    que.pop();
                    for (auto it : node->children)
                        que.push(it);
                }
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    递归前序遍历:

    class Solution {
    private:
        int result{0};
    public:
        void getDepth(Node *node, int depth)
        {
            result = depth > result ? depth : result;
            if (node->children.empty()) return;
            for (auto it : node->children)
            {
                ++depth;
                getDepth(it, depth);
                --depth;
            }
            return;
        }
        int maxDepth(Node* root) {
            if (root == nullptr) return 0;
            getDepth(root, 1);
            return result;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    递归后序遍历:

    class Solution {
    public:
        int getheight(Node *root)
        {
            if (root == nullptr) return 0;
            int depth = 0;
            for (auto it : root->children)
                depth = max(getheight(it), depth);
            return 1 + depth;
        }
    
        int maxDepth(Node* root) {
            return getheight(root);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    111.二叉树的最小深度

    思路:层序依然是模板题,可以从前序遍历和后序遍历来考虑。
    题解:
    层序遍历:

    class Solution {
    public:
        int minDepth(TreeNode* root) {
            queue<TreeNode*> que;
            int depth{0};
            if (root != nullptr) que.push(root);
            while(!que.empty())
            {
                int size = que.size();
                ++depth;
                for (int i = 0; i < size; ++i)
                {
                    TreeNode* node = que.front();
                    que.pop();
                    if (!node->left && !node->right) return depth;
                    if (node->left) que.push(node->left);
                    if (node->right) que.push(node->right);
                }
            }
            return depth;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    前序遍历:

    class Solution {
    public:
        int result{INT_MAX};
        void getDepth(TreeNode* node, int depth)
        {
            if (node == nullptr) return;
            if (node->left == nullptr && node->right == nullptr)
                result = depth < result ? depth : result;
            if (node -> left)
            {
                ++depth;
                getDepth(node->left, depth);
                --depth;
            }
            if (node -> right)
            {
                ++depth;
                getDepth(node->right, depth);
                --depth;
            }
            return;
        }
        int minDepth(TreeNode* root) {
            if (root == nullptr) return 0;
            getDepth(root, 1);
            return result;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    后序遍历:

    class Solution {
    public:
        int minDepth(TreeNode* root) {
            if (root == nullptr) 
                return 0;
            if (root->left == nullptr && root -> right == nullptr) 
                return 1;
            if (root->left == nullptr) 
                return 1 + minDepth(root->right);
            if (root -> right == nullptr) 
                return 1 + minDepth(root->left);
            return 1 + min(minDepth(root->left), minDepth(root->right));
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值

    举报

    选择你想要举报的内容(必选)
    • 内容涉黄
    • 政治相关
    • 内容抄袭
    • 涉嫌广告
    • 内容侵权
    • 侮辱谩骂
    • 样式问题
    • 其他
    点击体验
    DeepSeekR1满血版
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回顶部

    登录后您可以享受以下权益:

    ×