【题目】

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

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

【解题】

思路:

这道题要判断树形一样,在这个基础上val一样。

共有5种树型:

1. node为null

2. node是leave

3. node只有左child

4. node只有右child

5. node有两个children

Base cases:

1和2是base case树形

先判断是不是两个node都为null,如果是,return true;

在判断是不是node一个为null一个不为null,如果是,return false;

如果两个node都是leaves, 判断val是不是相等,等则true,不等则false;

Recursive cases:

3, 4和5是recursive case树形

树形均为3: 判断val相等和左child相等

树形均为4: 判断val相等和右child相等

树形均为5: 判断val相等, 左child相等和右child相等

其他:树形不一样

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// base case:
// two nodes are both null: true
// one is null and another is not: false
// both are leaves: check the val of the two nodes
if (p == null && q == null) {
return true;
} else if (p == null || q == null) {
return false;
} else if (p.left == null && q.left == null && p.right == null && q.right == null) {
if (p.val == q.val) {
return true;
} else {
return false;
} // recursive case:
} else if (p.left == null && q.left == null && p.right != null && q.right != null) {
return p.val == q.val && isSameTree(p.right, q.right); } else if (p.left != null && q.left != null && p.right == null && q.right == null) {
return p.val == q.val && isSameTree(p.left, q.left); } else if (p.left != null && q.left != null && p.right != null && q.right != null) {
return (p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)); } else {
return false;
}
}
}

100. Same Tree的更多相关文章

  1. 100. Same Tree(C++)

    100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...

  2. LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

    100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...

  3. 100.Same Tree(E)

    100. same tree 100. Same Tree Given two binary trees, write a function to check if they are the same ...

  4. <LeetCode OJ> 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  5. LeetCode 100. Same Tree (判断树是否完全相同)

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  6. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. LeetCode之100. Same Tree

    ------------------------------------------ 递归比较即可 AC代码: /** * Definition for a binary tree node. * p ...

  9. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

随机推荐

  1. boost学习笔记(七)---date_time库

    date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...

  2. mysql 5.7.16安装与给远程连接权限

    ZIP Archive版是免安装的.只要解压就行了.不需要安装.我的放在d盘啦. 1.配置: 也就是my.ini文件的由来. 把my-default.ini(此文件是解压之后,自带的)这个文件复制一下 ...

  3. Hadoop 简介

    一个开源的,高可靠,可扩展的分布式计算框架 解决的问题 1 海量数据的存储(HDFS) 2海量数据的分析(Mapreduce) 3 分布式资源调度 (Yarn) 应用场景 日志分析,基于海量数据的在线 ...

  4. Python之操作Redis、 RabbitMQ、SQLAlchemy、paramiko、mysql

    一.Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.Redis是一个key-value存储系统.和 ...

  5. NuGet安装及使用教程

    Nuget是一个.NET平台下的开源的项目,它是Visual Studio的扩展.在使用Visual Studio开发基于.NET Framework的应用时,Nuget能把在项目中添加.移除和更新引 ...

  6. Bean熟悉替换,只替换部分属性,其他属性值不改变

    Bean熟悉替换,只替换部分属性,其他属性值不改变 需要加入:asm.jar  cglib-2.1.jar,用来map和bean之间的转换(比spring和反射的效率好,因为加入了缓存) packag ...

  7. php : MVC 演示(使用单例工厂)

    此例子是MVC的简单应用, 要达到的效果如下: 用户列表: 姓名 年龄 学历 兴趣 出生地 账号创建时间 操作 keen 20 高中 篮球,足球 广东 2016-11-08 10:00:31 删除 a ...

  8. npm相关

    npm list -g --depth 0 : -g 列出所有全局模块,--depth 使列表只列出简略信息,如包名.版本号

  9. Android微信智能心跳方案 (转)

    原创 2015-08-17 phoenix WeMobileDev 前言:在13年11月中旬时,因为基础组件组人手紧张,Leo安排我和春哥去广州轮岗支援.刚到广州的时候,Ray让我和春哥对Line和W ...

  10. position之fixed固定定位、absolute绝对定位和relative相对定位

    什么是层模型? 什么是层布局模型?层布局模型就像是图像软件PhotoShop中非常流行的图层编辑功能一样,每个图层能够精确定位操作,但在网页设计领域,由于网页大小的活动性,层布局没能受到热捧.但是在网 ...