101. Symmetric Tree\104. Maximum Depth of Binary Tree\111. Minimum Depth of Binary Tree

2023-10-30

101. Symmetric Tree

题目描述

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3

代码实现

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSym(TreeNode *l, TreeNode *r) {
        if(l && r) {
            if(l->val == r->val) {
                if(isSym(l->right, r->left) && isSym(l->left, r->right))
                    return true;
                else
                    return false;
            }
            else return false;
        }
        else if((!l && r) || (l && !r))
            return false;
        else
            return true;
    }

    bool isSymmetric(TreeNode* root) {
        if(root) {
            return isSym(root->left, root->right);
        }
        else
            return true;
    }
};

104. Maximum Depth of Binary Tree

题目描述

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

代码实现

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void culMaxDepth(TreeNode *root, int &max_dep) {
        if(!root) return;
        else {
            if(root->val > max_dep) max_dep = root->val;
            if(root->left) {
                root->left->val =  root->val + 1;
                culMaxDepth(root->left, max_dep);
            }
            if(root->right) {
                root->right->val = root->val + 1;
                culMaxDepth(root->right, max_dep);
            }
        }
    }

    int maxDepth(TreeNode* root) {
        int max_dep = 0;
        if(root) {
            root->val = 1;
            culMaxDepth(root, max_dep);
        }    
        return max_dep;
    }
};

DFS的做法:一行搞定,简直diao

int maxDepth(TreeNode *root)
{
    return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
}

这里使用深度优先的算法,深度优先更加简单,更加容易实现。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode *root)
    {
        if(root == NULL) return 0;
        int res = 0;
        queue<TreeNode *> q;
        q.push(root);
        while(!q.empty()) {
            ++ res;
            for(int i = 0, n = q.size(); i < n; ++ i) {
                TreeNode *p = q.front(); q.pop();
                if(p -> left != NULL) q.push(p -> left);
                if(p -> right != NULL) q.push(p -> right);
            }
        }
        return res;
    }
};

111. Minimum Depth of Binary Tree

题目描述

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

代码实现

使用广度优先的算法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        int res = 0;
        if(!root) return res;
        queue<TreeNode*> tq;
        tq.push(root);
        while(!tq.empty()) {
            res++;
            int sz = tq.size(); 
            for(int i = 0; i < sz; i++) {
                TreeNode* tmp = tq.front();
                tq.pop();
                if(!tmp->left && !tmp->right) return res;
                if(tmp->left) tq.push(tmp->left);
                if(tmp->right) tq.push(tmp->right);
            }
        }

        return res;
    }
};

DFS的做法:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode *root) {
        if(!root) return 0;
        if(!root->left) return 1 + minDepth(root->right);
        if(!root->right) return 1 + minDepth(root->left);
        return 1+min(minDepth(root->left),minDepth(root->right));
    }
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

101. Symmetric Tree\104. Maximum Depth of Binary Tree\111. Minimum Depth of Binary Tree 的相关文章

  • U-boot中LPDDR4关键参数的意义

    LPDDR4关键参数意义 usr bin env python3 import struct 手动配置 0 disable 1 enable manual config 0 DDR的数据位宽 data width 32 channel个数
  • vue使用高德地图点聚合,点击显示弹窗

    高德地图点聚合 点击地图上的点展示弹窗 再根据不同类型展示不同的图片 html div class public map div js 官网需要的数据格式 export default data return maplist map nul
  • Vue-admin-template笔记(一)

    Vue admin template项目 一 关于Vue admin template 1 1 介绍 vue element admin 是一个后台前端解决方案 它基于 vue 和 element ui实现 可以把 vue element
  • 图片转换成16进制数据,在显示成图片

    1 目的 在串行 或者网络通信的时候 往往需要把图片解析成16进制的数据 方便数据的传输 而在另一端接收到数据后 在将接收到的数据显示成图片 2 代码 include mainwindow h include ui mainwindow h

随机推荐

  • 机械革命蛟龙16Windows重装流程

    1 必要文件的拷贝 将桌面以及D盘一些重要文件拷贝入移动硬盘 2 重装 利用的是Win11自带的系统重装功能 设置 gt Windows更新 gt 高级选项 gt 恢复 gt 重置此电脑中的初始化电脑 gt 删除所有内容 gt 本地重新安装
  • rabbitmq安装

    1 安装 1 1 安装Erlang gt yum install y gcc gcc c glibc devel make ncurses devel openssl devel autoconf java 1 8 0 openjdk de
  • Linux下安装mysql

    1 下载 http dev mysql com downloads mysql 或者使用wget下载 wget http dev mysql com get Downloads MySQL 5 6 MySQL 5 6 22 1 el6 i6
  • 【MySQL入门到精通-黑马程序员】MySQL基础篇-概述及MySQL环境配置

    文章目录 前言 一 MySQL概述 1 1 数据库相关概念 1 2 MySQL数据库 二 数据模型 三 总结 前言 本专栏文章为观看黑马程序员 MySQL入门到精通 所做笔记 课程地址在这 如有侵权 立即删除 一 MySQL概述 1 1 数
  • SCI数据库使用手册(无图版)

    含图笔记在有道云笔记中 https note youdao com s O3YuEZJc 文章目录 1 SCI数据库的简介 对应作业3的第一问 2 打开SCI检索 对应作业3的第一问 3 关键词检索 以KBQA为例 对应作业3的第二问 3
  • Android获取当前位置(GPS和网络定位)

    1 比较 GPS准确度高但耗电多 网络定位耗电少但准确度低 2 代码 添加权限 AndroidManifest xml
  • vue使用百度地图(标点、点击后展示弹框、多个标点、点聚合)

    安装百度地图 npm install vue baidu map save 在百度地图开放平台申请AK 全局注册 在项目的main js中引入 import Vue from vue import baiduMap from vue bai
  • Shell脚本编写教程【五】——Shell 基本运算符

    Shell脚本编写教程 五 Shell 基本运算符 目录 https blog csdn net shn111 article details 131590488 参考教程 https www runoob com linux linux
  • 练习-Java类和对象之包的定义

    第1关 练习 Java类和对象之包的定义 任务描述 编程要求 测试说明 任务描述 本关任务 定义一个电影类和一个电影测试类 在电影测试类中通过对象完成成员变量和成员方法的使用 编程要求 仔细阅读右侧编辑区内给出的代码框架及注释 在 Begi
  • 【Docker】Docker网络

    1 配置容器网络 1 通过实训平台进入到操作系统界面 在 后输入docker run i t d net none ubuntu bin bash命令 启动一个 bin bash容器 示例代码如图1所示 2 在 后输入docker ps a
  • ant-vue中的a-icon使用方法

    Ant Design 图标库 直接引入的使用方式 你直接点击相应的图标会自动将图标名称复制到你的剪切板上
  • Unity3D游戏开发介绍

    Unity3D游戏开发介绍 Unity3D Unity是实时3D互动内容创作和运营平台 包括游戏开发 美术 建筑 汽车设计 影视在内的所有创作者 借助Unity将创意变成现实 Unity平台提供一整套完善的软件解决方案 可用于创作 运营和变
  • CenOS7 下安装wget命令

    1 安装vsfdp yum y install vsftpd 2 关闭防火墙 systemctl stop firewalld service 3 将本机目录下的wget安装文件上传至虚拟机 scp wget 1 14 18 el7 6 1
  • 案例:用户信息列表展示

    1 需求 用户信息的增删改查操作 2 设计 1 技术选型 Servlet JSP MySQL JDBCTempleat Duird BeanUtilS tomcat 2 数据库设计 create database day17 创建数据库 u
  • uva11292 Dragon of Loowater (水题)

    include
  • 电脑怎么在Bios中开启虚拟化

    1 开机按F1进入BIOS Configuration Secuity 2 然后选择Virtulize 或者Intel Virtual Technolody 设置成Enable 3 F10保存 重启
  • String类为什么是不可变的

    String StringBuilder StringBuffer是经常考的东西 其中 String是不可变的 为什么呢 简单解释如下 String类new了一个对象后 我们看到的该对象只是引用 存放了真正内存的地址 并不是真的内存值 如果
  • 500.键盘行 693.交替位二进制数(java实现)

    键盘行 题目 给定一个单词列表 只返回可以使用在键盘同一行的字母打印出来的单词 键盘如下图所示 示例1 输入 Hello Alaska Dad Peace 输出 Alaska Dad 注意 你可以重复使用键盘上同一字符 你可以假设输入的字符
  • 瑞芯微RK3399交叉编译MPP

    上一篇介绍了如何在ubuntu下搭建瑞芯微RK3399的检查编译环境 现在就要开始交叉编译MPP来进行对视频的硬编硬解 这里RK3399用的aarch64架构芯片 上面跑的linux 如果编译android版 需要其他配置 这里主要对lin
  • 101. Symmetric Tree\104. Maximum Depth of Binary Tree\111. Minimum Depth of Binary Tree

    Symmetric Tree 题目描述 代码实现 Maximum Depth of Binary Tree 题目描述 代码实现 Minimum Depth of Binary Tree 题目描述 代码实现 101 Symmetric Tre