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的吃透了 足以应付非算法岗 ...
随机推荐
- DEX文件解析--4、dex类的类型解析
一.前言 前几篇系列文章链接: DEX文件解析---1.dex文件头解析 DEX文件解析---2.Dex文件checksum(校验和)解析 DEX文件解析--3.dex文件 ...
- Ethical Hacking - NETWORK PENETRATION TESTING(9)
WEP Cracking Packet Injection What if the AP was idle, or had no clients associated with it? In this ...
- noi linux gedit 配置(c++环境)
基本配置 方法一 查看所有命令: gsettings list-recursively | grep -i gedit 命令解释 gsettings set org.gnome.gedit.prefe ...
- 怎么理解Python迭代器与生成器?
怎么理解Python迭代器与生成器?在Python中,使用for ... in ... 可以对list.tuple.set和dict数据类型进行迭代,可以把所有数据都过滤出来.如下: ...
- python dict乱码如何解决
定义字典并直接输出,结果输出结果中文是乱码展示 d={'name':'lily','age':18,'sex':'女','no':1121} print d 输出结果: {'age': 18, 'no ...
- 【原创】xenomai内核解析--同步互斥机制(一)--优先级倒置
版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 目录 一.xenomai 资源管理简要 二.优先级倒 ...
- Python3网络爬虫开发实战PDF高清完整版免费下载|百度云盘
百度云盘:Python3网络爬虫开发实战高清完整版免费下载 提取码:d03u 内容简介 本书介绍了如何利用Python 3开发网络爬虫,书中首先介绍了环境配置和基础知识,然后讨论了urllib.req ...
- 大数据框架Hive优化方法
常规调优手段 Fetch抓取 某些情况查询不必用MapReduce计算,比如select*,可以直接读取文件 本地模式 有时数据量比较小,hive可以通过本地模式在单台机器上处理所有任务,对于小数据集 ...
- Django学习路26_转换字符串大小写 upper,lower
在 urls 中注册 url(r'getstr',views.getstr) 在 views.py 中添加函数 def getstr(request): string = 'abc' string_2 ...
- Numpy访问数组元素
import numpy as np n = np.array(([1,2,3],[4,5,6],[7,8,9])) ''' array([[1, 2, 3], [4, 5, 6], [7, 8, 9 ...