代码随想录算法训练营Day14 | LeetCode | 226.翻转二叉树 | 101. 对称二叉树 | 104. 二叉树的最大深度 | 111. 二叉树的最小深度

今日任务

226.翻转二叉树

  • 题目链接: https://leetcode.cn/problems/invert-binary-tree/description/
  • 题目描述
    在这里插入图片描述

Code 递归 Or 迭代

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

        // root->left = right;
        // root->right = left;
        // invertTree(root->right);
        // swap(root->left, root->right);
        // invertTree(root->right);
        vector<TreeNode *> cur = {root};
        while(!cur.empty()){
            vector<TreeNode *> next;
            for(auto &node : cur){
                swap(node->left, node->right);
                if(node->left){
                    next.emplace_back(node->left);
                }
                if(node->right){
                    next.emplace_back(node->right);
                }
            }
            cur = move(next);
        }
        return root;
    }
};

101. 对称二叉树

  • 题目链接: https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
  • 题目描述

在这里插入图片描述

Code 迭代 Or 递归

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr){
            return true;
        }
        // function<bool(const vector<TreeNode*> &)> is_symmetry = 
        // [&](const vector<TreeNode *> &cur) ->bool{
        //     int left = 0, right = cur.size() - 1;
        //     while(left < right){
        //         if(cur[left] && cur[right]){
        //             if(cur[left]->val != cur[right]->val){
        //                 return false;
        //             }
        //         }else if(cur[left] != cur[right]){
        //             return false;
        //         }
        //         left++;
        //         right--;
        //     }
        //     return true;
        // };
        // vector<TreeNode *> cur = {root};
        // while(!cur.empty()){
        //     vector<TreeNode *> next;
        //     for(const auto &node : cur){
        //         if(node){
        //             next.emplace_back(node->left);
        //             next.emplace_back(node->right);
        //         }
        //     }
        //     if(!is_symmetry(next)){
        //         return false;
        //     }
        //     cur = move(next);
        // }
        // return true;

        function <bool(TreeNode *, TreeNode *)> is_symmetry 
        = [&](auto p, auto q)->bool{
            if(p == nullptr || q == nullptr){
                return p == q;
            }
            return p->val == q->val && is_symmetry(p->left, q->right) && is_symmetry(p->right, q->left);
        };
        return is_symmetry(root->left, root->right);
    }
};

104. 二叉树的最大深度

  • 题目链接: https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/
  • 题目描述

在这里插入图片描述

Code

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == nullptr){
            return 0;
        }
        int lmax = maxDepth(root->left);
        int rmax = maxDepth(root->right);
        return 1 + max(lmax, rmax);
    }
};

111. 二叉树的最小深度

  • 题目链接: https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
  • 题目描述
    -在这里插入图片描述

Code 三种递归

class Solution {
public:
    int minDepth(TreeNode* root) {
        // if(root == nullptr){
        //     return 0;
        // }
        // int ldepth = minDepth(root->left);
        // int rdepth = minDepth(root->right);
        // return ldepth && rdepth ? min(ldepth, rdepth) + 1 : 1 + ldepth + rdepth;


        // int ans = INT_MAX;
        // function<void(TreeNode *, int)> dfs = [&] (TreeNode *node, int depth) -> void{
        //     if(node == nullptr || ++depth >= ans){
        //         return ;
        //     }
        //     if(node->left == node->right){
        //         ans = depth;
        //         return;
        //     }
        //     dfs(node->left, depth);
        //     dfs(node->right, depth);
        // };
        // dfs(root, 0);
        // return root ? ans : 0;

        if(root == nullptr){
            return 0;
        }
        if(root->right == nullptr){
            return 1 + minDepth(root->left);
        }
        if(root->left == nullptr){
            return 1 + minDepth(root->right);
        }
        return 1 +min(minDepth(root->left), minDepth(root->right));
    }
};
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值