代码随想录打卡第十四天

news/2024/7/8 4:49:30 标签: 算法, 数据结构, c++, leetcode

代码随想录–二叉树部分

day14 二叉树第二天


文章目录

  • 代码随想录--二叉树部分
  • 一、力扣226--反转二叉树
  • 二、力扣101--对称二叉树
  • 三、力扣104--二叉树的最大深度
  • 四、力扣111--二叉树的最小深度


一、力扣226–反转二叉树

代码随想录题目链接:代码随想录

给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。

递归法可以秒了

代码如下:

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

二、力扣101–对称二叉树

代码随想录题目链接:代码随想录

给你一个二叉树的根节点 root , 检查它是否轴对称。

这道题用递归做的话,反而不太好写

因为要检查是否轴对称,所以其实迭代需要传入两颗树的root,比较两棵树是否对称

而仅在这两个root不为空且数字一样时,再进行下一步的检查

也就是递归检查左树的左节点和右树的右节点是否一样,以及左树的右节点和右树的左节点是否一样

代码如下:

class Solution {
public:
    bool checkThem(TreeNode * leftN, TreeNode * rightN)
    {
        if(leftN != nullptr && rightN == nullptr) return false;
        else if(leftN == nullptr && rightN != nullptr) return false;
        else if(leftN == nullptr && rightN == nullptr) return true;
        else if(leftN->val != rightN ->val) return false;

        bool outside = checkThem(leftN->left, rightN->right);
        bool inside = checkThem(leftN->right, rightN->left);
        return outside && inside;
    }
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        return checkThem(root->left, root->right);
    }
};

用遍历法写就会简单的多,一层一层地把元素入队,每次出两个比较,都一样且队空就是对称,否则不对称

代码如下:

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        queue<TreeNode*> checkQue;
        checkQue.push(root->left);
        checkQue.push(root->right);
        while(!checkQue.empty())
        {
            TreeNode * leftN = checkQue.front();
            checkQue.pop();
            TreeNode * rightN = checkQue.front();
            checkQue.pop();

            if(!leftN && !rightN) continue; // both nullptr
            else if(!leftN && rightN) return false;
            else if(leftN && !rightN) return false;
            else if(leftN->val != rightN->val) return false;

            checkQue.push(leftN->left);
            checkQue.push(rightN->right);
            checkQue.push(leftN->right);
            checkQue.push(rightN->left);
        }
        return true;
    }
};

附加题:力扣100–相同的树

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(!p && !q) return true;
        else if(!p && q) return false;
        else if(!q && p) return false;
        else if(p->val != q->val) return false;

        return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
    }
};

力扣572–另一颗树的子树,层序遍历+相同树判断

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(!p && !q) return true;
        else if(!p && q) return false;
        else if(!q && p) return false;
        else if(p->val != q->val) return false;

        return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        queue<TreeNode *> que;
        que.push(root);
        while(!que.empty())
        {
            TreeNode * curr = que.front(); que.pop();
            if(isSameTree(curr, subRoot)) return true;
            if(curr->left) que.push(curr->left);
            if(curr->right) que.push(curr->right);
        }
        return false;
    }
};

三、力扣104–二叉树的最大深度

代码随想录题目链接:代码随想录

给定一个二叉树 root ,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

第一反应就是层序遍历,每次遍历+1即可

代码如下:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth = 0;
        if(!root) return depth;
        queue<TreeNode *> que;
        que.push(root);
        while(!que.empty())
        {
            int length = que.size();
            depth ++;
            for(int i = 0; i < length; i++)
            {
                TreeNode * curr = que.front(); que.pop();
                if(curr->left) que.push(curr->left);
                if(curr->right) que.push(curr->right);
            }
        }
        return depth;
    }
};

迭代法做也是可以的,迭代遍历左右树层数,选最大的那个,每层迭代向上返回时+1即可记录迭代层数

代码如下:

class Solution {
public:
    int getDepth(TreeNode * curr)
    {
        if(!curr) return 0;

        int leftDepth = getDepth(curr->left);
        int rightDepth = getDepth(curr->right);
        return 1+max(leftDepth, rightDepth);
    }
    int maxDepth(TreeNode* root) {
        return getDepth(root);
    }
};

可以简洁,但是为了方便回忆,就这么写吧

附加题:力扣559–N叉树的最大深度

class Solution {
public:
    int maxDepth(Node* root) {
       if(!root) return 0; 
       int depth = 0;
       queue<Node *> que;
       que.push(root);
       while(!que.empty())
       {
            int length = que.size();
            depth ++;
            for(int i = 0; i < length; i ++)
            {
                Node * curr = que.front(); que.pop();
                for(auto child : curr->children)
                {
                    if(child) que.push(child);
                }
            }
       }
       return depth;
    }
};

class Solution {
public:
    int maxDepth(Node* root) {
        if(!root) return 0;
        int depth = 0;
        for(auto child : root->children)
        {
            int curr_depth = maxDepth(child);
            depth = depth > curr_depth ? depth : curr_depth;
        }
        return depth + 1;
    }
};

四、力扣111–二叉树的最小深度

代码随想录题目链接:代码随想录

给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。

跟最大深度不同的是,返回时需要检查一下这个节点是否有左右树,如果是单侧树的话,不能单纯返回1+min的结果

只有该节点左右树都有或者都没有的情况下才能返回1+min,因为此时的节点才有可能是叶子节点

代码如下:

class Solution {
public:
    int getDepth(TreeNode * curr)
    {
        if(!curr) return 0;
        
        int leftDepth = getDepth(curr->left);
        int rightDepth = getDepth(curr->right);

        if(!curr->left && curr->right) return 1 + rightDepth;
        if(!curr->right && curr->left) return 1 + leftDepth;

        return 1 + min(leftDepth, rightDepth);
    }
    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

迭代法:正常的层序遍历,但是在检查到第一个叶子节点的时候就return,下面的就不用遍历了

因为是一层一层的遍历,每次遍历都是同一层的叶子节点,也就无所谓是哪一个,深度是一样的

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root) return 0;
        int depth = 0;
        queue<TreeNode *> que;
        que.push(root);
        while(!que.empty())
        {
            int length = que.size();
            depth ++;
            for(int i = 0; i < length; i ++)
            {
                TreeNode * curr = que.front(); que.pop();
                if(curr->left) que.push(curr->left);
                if(curr->right) que.push(curr->right);
                if(!curr->left && !curr->right) return depth;
            }
        }
        return depth;
    }
};

http://www.niftyadmin.cn/n/5536590.html

相关文章

C#委托事件的实现

1、事件 在C#中事件是一种特殊的委托类型&#xff0c;用于在对象之间提供一种基于观察者模式的通知机制。 1.1、事件的发送方定义了一个委托&#xff0c;委托类型的声明包含了事件的签名&#xff0c;即事件处理器方法的签名。 1.2、事件的订阅者可以通过运算符来注册事件处理器…

(已解决)Adobe Flash Player已不再受支持

文章目录 前言解决方案 前言 一般来说&#xff0c;很少遇到官方网站使用Adobe Flash Player来进行录用名单公示了。但是&#xff0c;今天就偏偏遇到一次&#xff0c; 用谷歌浏览器打不开&#xff0c; 点了没有反应&#xff0c;用其他的浏览器&#xff0c;例如windows自带的那…

react native优质开源项目

React Native 是一个非常流行的用于构建跨平台移动应用程序的框架&#xff0c;开源社区贡献了许多优质的项目和库。以下是一些备受认可的 React Native 开源项目&#xff0c;适合用来学习和参考&#xff1a; ### 1. **React Native Elements** [React Native Elements](https:…

SwiftUI八与UIKIT交互

代码下载 SwiftUI可以在苹果全平台上无缝兼容现有的UI框架。例如&#xff0c;可以在SwiftUI视图中嵌入UIKit视图或UIKit视图控制器&#xff0c;反过来在UIKit视图或UIKit视图控制器中也可以嵌入SwiftUI视图。 本文展示如何把landmark应用的主页混合使用UIPageViewController和…

yolo-seg模型后处理

yolo-seg模型mask处理 YOLOv8-seg模型一共有两个输出。第一个输出是“output0”&#xff0c;它的类型是float32[1,116,8400]。在这个输出中&#xff0c;前84个列与YOLOv8目标检测模型的输出定义相同&#xff0c;包括cx、cy、w、h这4项&#xff0c;再加上80个类别的分数。而后面…

事务的特性-原子性(Atomicity)、一致性(Consistency)、隔离性(Asolation)、持久性(Durability)

一、引言 1、数据库管理系统DBMS为保证定义的事务是一个逻辑工作单元&#xff0c;达到引入事务的目的&#xff0c;实现的事务机制要保证事务具有原子性、一致性、隔离性和持久性&#xff0c;事务的这四个特性也统称为事务的ACID特性 2、当事务保持了ACID特性&#xff0c;才能…

记一次因ThreadPoolExecutor多线程导致服务器内存压满问题

经过下载服务器内存数据得知是通过多线程业务处理查询list集合数据没有得到正确释放导致的。 首先先了解一下list集合数据的存放和回收&#xff08;可能说的不对&#xff0c;请谅解【挠头】&#xff09; 存放&#xff1a; 当我们创建一个list或者从数据库查询出的数据用list集…

iOS项目怎样进行二进制重排

什么是二进制重排 &#xff1f; 在iOS项目中&#xff0c;二进制重排&#xff08;Binary Reordering 或者 Binary Rearrangement&#xff09;是一种优化技术&#xff0c;主要目的是通过重新组织应用程序的二进制文件中的代码和数据段&#xff0c;来提高应用程序的性能&#xff…