C#LeetCode刷题之#100-相同的树(Same Tree)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- 【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 ...
- 【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 ...
- C#LeetCode刷题之#101-对称二叉树(Symmetric Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4068 访问. 给定一个二叉树,检查它是否是镜像对称的. 例如,二 ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- LeetCode 热题 HOT 100(05,正则表达式匹配)
LeetCode 热题 HOT 100(05,正则表达式匹配) 不够优秀,发量尚多,千锤百炼,方可成佛. 算法的重要性不言而喻,无论你是研究者,还是最近比较火热的IT 打工人,都理应需要一定的算法能力 ...
- 看完互联网大佬的「LeetCode 刷题手册」, 手撕了 400 道 Leetcode 算法题
大家好,我是 程序员小熊 ,来自 大厂 的程序猿.相信绝大部分程序猿都有一个进大厂的梦想,但相较于以前,目前大厂的面试,只要是研发相关岗位,算法题基本少不了,所以现在很多人都会去刷 Leetcode ...
- LeetCode刷题模板(1):《我要打10个》之二分法
Author : 叨陪鲤 Email : vip_13031075266@163.com Date : 2021.01.23 Copyright : 未 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
随机推荐
- OSCP Learning Notes - File Transfers(2)
Metasploit Target Server: Kioptrix Level 1 (1) Start the Metasploit on Kali Linux. (2) Set the modul ...
- 使用Azure DevOps Pipeline实现.Net Core程序的CI
上次介绍了Azure Application Insights,实现了.net core程序的监控功能.这次让我们来看看Azure DevOps Pipeline功能.Azure DevOps Pip ...
- .NET 使用sock5做代理(不是搭建服务端)
在日常开发中经常会遇到这些需求,爬取数据,都知道现在通常用python爬取是很方便的,但是免不了还有很多伙伴在用NET来爬取,在爬取数据的时候我们知道需要使用代理服务器,如果不用代理,你的IP很有可能 ...
- C#计算数组的算术平均数、几何平均数、调和平均数、平方平均数和中位数
1.函数实现 0)打印数组 /// <summary> /// 打印数组 /// </summary> /// <param name="arr"&g ...
- 你的JavaBean是否真的需要实现Serializable
众所周知 如果一个对象需要进行网络传输,那么该对象就需要实现Serializable接口,为了防止反序列失败,该对象需提供一个默认的serialVersionUID(该值在反序列化的时候会进行校验校验 ...
- 谁能告诉我如何通过Jenkins完成分布式环境搭建并执行自动化脚本
今天我们接着昨天的内容,看一看如何完成Jenkins分布式环境的搭建和使用,因为我之前也是自己一个人摸索的,如果有不对的地方,请各位看官私信指出. 新增分布式部署节点 在系统管理/节点管理中点击新建 ...
- django表单使用
一.表单常用字段类型及参数 表单可以自动生成html代码,每一个字段默认有一个html显示样式,大多数默认为输入框. 字段相当于正则表达式的集合,能够对表单传入的数据进行校验,并且某一部分校验失败时会 ...
- 人车识别实验丨华为ModelArts VS 百度Easy DL硬核体验
摘要:想了解时下流行的自动驾驶相关AI模型吗?接下来就用华为云的ModelArts和百度的Easy DL带你体验一下AI平台是怎么进行模型训练的. 华为ModelArts自动学习 VS 百度Easy ...
- git命令常用操作
第一步:拉取git上的文件(git clone 远程仓库地址) 第二步:在主目录下运行git base here,执行git status 第三步:添加文件到本地仓库(git add 文件)之后,再次 ...
- Django学习路21_views函数中定义字典及html中使用类实例对象的属性及方法
创建 app6 在项目的 settings 中进行注册 INSTALLED_APPS 里面添加 'app6.apps.App6Config' 在 app6 的models.py 中创建数据表 clas ...