问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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种算法的时间复杂度均为:  。

最新文章

  1. WPF 数据绑定 1_1 基础知识&绑定到元素属性
  2. SpringMVC使用的几个要点
  3. win7环境下安装运行gotour【转载整理】
  4. 跟着百度学PHP[4]OOP面对对象编程-13-魔术方法__set(),__get(),__isset(),__unset()
  5. 6天的巴厘岛旅行 I love Bali
  6. [CareerCup] 5.2 Binary Representation of Real Number 实数的二进制表示
  7. JdbcTemplate主要提供以下五类方法:
  8. oracle LogMiner配置使用
  9. 通过百度获取IP地址对应的经纬度
  10. WPF笔记(1.3 属性元素)——Hello,WPF!
  11. MySQL --概述--
  12. 避免Toast重复弹出
  13. ROS机器人程序设计(原书第2版)补充资料 kinetic
  14. C#事件委托概念
  15. 关于HTML和CSS一些鸡零狗碎的事
  16. SQL中的排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
  17. ES6checker ES6浏览器检测
  18. vue WepApp 音乐App前的准备
  19. linux 内核分析工具 Dtrace、SystemTap、火焰图、crash等
  20. 【Java】JavaIO(一)、基础知识

热门文章

  1. Spring Boot Redis 实现分布式锁,真香!!
  2. Python Ethical Hacking - TROJANS Analysis(5)
  3. Dresdon二次开发
  4. 集训作业 洛谷P1010 幂次方
  5. create-react-app中的babel配置探索
  6. 写给程序员的机器学习入门 (八) - 卷积神经网络 (CNN) - 图片分类和验证码识别
  7. 深入浅出系列第一篇(设计模式之单一职责原则)——从纯小白到Java开发的坎坷经历
  8. Linux重定向用法详解
  9. 第二部分_Mac技巧
  10. PHP xml_parser_set_option() 函数