问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4066 访问。

给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

输入:       1         1

               / \       / \

             2   3   2   3

[1,2,3],   [1,2,3]

输出: true

输入:      1          1

               /           \

             2             2

[1,2],     [1,null,2]

输出: false

输入:       1         1

               / \       / \

             2   1    1   2

[1,2,1],   [1,1,2]

输出: false


Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Input:     1         1

              / \       / \

            2   3   2   3

[1,2,3],   [1,2,3]

Output: true

Input:     1         1

               /           \

             2             2

[1,2],     [1,null,2]

Output: false

Input:     1         1

               / \       / \

             2   1   1    2

[1,2,1],   [1,1,2]

Output: false


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4066 访问。

public class Program {

    public static void Main(string[] args) {
var p = new TreeNode(1) {
left = new TreeNode(2)
}; var q = new TreeNode(1) {
left = new TreeNode(2)
}; var res = IsSameTree(p, q);
Console.WriteLine(res); q.right = new TreeNode(6); res = IsSameTree2(p, q);
Console.WriteLine(res); Console.ReadKey();
} public static void IsSame(TreeNode p, TreeNode q, ref bool isSameTree) {
//先序遍历分析法
if(p?.val != q?.val) {
isSameTree = false;
return;
}
if(p != null && q != null) {
if(p.left != null || q.left != null) {
IsSame(p.left, q.left, ref isSameTree);
}
if(p.right != null || q.right != null) {
IsSame(p.right, q.right, ref isSameTree);
}
}
return;
} public static bool IsSameTree(TreeNode p, TreeNode q) {
var isSameTree = true;
IsSame(p, q, ref isSameTree);
return isSameTree;
} public static bool IsSameTree2(TreeNode p, TreeNode q) {
//优雅递归法
//如果 p 和 q 同时为空,则肯定相
//已经到了递归最底层的调用,也就是递归的最基本情况
//属于递归的终止条件
//以下3行代码均是终止条件
if(p == null && q == null) return true;
//如果 p 为空,q 不为空,则肯定不相同
if(p == null) return false;
//如果 q 为空,p 不为空,则肯定不相同
if(q == null) return false;
//递归调用
return p.val == q.val &&
IsSameTree2(p.left, q.left) &&
IsSameTree2(p.right, q.right);
} public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4066 访问。

True
False

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#100-相同的树(Same Tree)的更多相关文章

  1. 【leetcode刷题笔记】Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. 【leetcode刷题笔记】Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. C#LeetCode刷题之#101-对称二叉树(Symmetric Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4068 访问. 给定一个二叉树,检查它是否是镜像对称的. 例如,二 ...

  4. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

  5. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  6. LeetCode 热题 HOT 100(05,正则表达式匹配)

    LeetCode 热题 HOT 100(05,正则表达式匹配) 不够优秀,发量尚多,千锤百炼,方可成佛. 算法的重要性不言而喻,无论你是研究者,还是最近比较火热的IT 打工人,都理应需要一定的算法能力 ...

  7. 看完互联网大佬的「LeetCode 刷题手册」, 手撕了 400 道 Leetcode 算法题

    大家好,我是 程序员小熊 ,来自 大厂 的程序猿.相信绝大部分程序猿都有一个进大厂的梦想,但相较于以前,目前大厂的面试,只要是研发相关岗位,算法题基本少不了,所以现在很多人都会去刷 Leetcode ...

  8. LeetCode刷题模板(1):《我要打10个》之二分法

    Author       :  叨陪鲤 Email         : vip_13031075266@163.com Date          : 2021.01.23 Copyright : 未 ...

  9. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

随机推荐

  1. 什么是控制反转(IoC)?什么是依赖注入(DI)?以及实现原理

    ​ IoC不是一种技术,只是一种思想,一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合.更优良的程序.传统应用程序都是由我们在类内部主动创建依赖对象,从而导致类与类之间高耦合,难于测试:有了 ...

  2. kubernetes系列(十七) - 通过helm安装dashboard详细教程

    1. 前提条件 2. 配置https证书为secret 3. dashboard安装 3.1 helm拉取dashboard的chart 3.2 配置dashboard的chart包配置 3.3 he ...

  3. 《python编程从入门到实践》2.3字符串

    书籍<python编程从入门到实践> 2.3字符串 知识模块 print()函数,函数名称突出为蓝色,输出括号内的变量或者字符创. 变量名的命名:尽量小写字母加下划线并且具有良好的描述性, ...

  4. css : 使用浮动实现左右各放一个元素时很容易犯的错误

    比如说,有一个div,我想在左侧和右侧各方一个元素. 如果不想用flex,那就只能用浮动了. ... <div class="up clearfix"> <h6& ...

  5. JAVA I/O基本操作

    JAVA I/O基本操作 JAVA文件操作 JAVA字节流 JAVA字符流 JAVA缓存流 JAVA对象流 JAVA数据流 本文主要借鉴以下博客和网站: how2j.cn 深入理解java中的I/O ...

  6. 最小割&网络流应用

    重要链接 基础部分链接 : 二分图 & 网络流初步 zzz大佬博客链接 : 网络流学习笔记 重点内容:最小割二元关系新解(lyd's ppt) 题目:网络流相关题目 lyd神犇课件链接 : 网 ...

  7. nginx访问日志分析,筛选时间大于1秒的请求

    处理nginx访问日志,筛选时间大于1秒的请求   #!/usr/bin/env python ''' 处理访问日志,筛选时间大于1秒的请求 ''' with open('test.log','a+' ...

  8. Android实现EditText插入表情、超链接等格式

    参考:https://www.jianshu.com/p/84067ad289d2 参考:https://www.jianshu.com/p/d82ac2edc7e8

  9. log4j2.xml配置使用

    jar包: log4j-api-2.10.0.jar log4j-core-2.10.10.jar log4j-1.2-api-2.10.0.jar log4j-slf4j-impl-2.10.10. ...

  10. java 控制语句、数组、方法

    一.控制语句 1.if 语句 if语句是指如果满足某种条件,就进行某种处理. 流程图: 2. if…else语句 语法格式: if (判断条件){ 执行语句1 …… }else{ 执行语句2 …… } ...