100. Same Tree

Easy

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.

Example 1:

Input:     1         1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true

Example 2:

Input:     1         1
/ \
2 2 [1,2], [1,null,2] Output: false

Example 3:

Input:     1         1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false
package leetcode.easy;

import java.util.ArrayDeque;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
} public class SameTree {
@org.junit.Test
public void test1() {
TreeNode tn11 = new TreeNode(1);
TreeNode tn12 = new TreeNode(2);
TreeNode tn13 = new TreeNode(3);
tn11.left = tn12;
tn11.right = tn13;
tn12.left = null;
tn12.right = null;
tn13.left = null;
tn13.right = null; TreeNode tn21 = new TreeNode(1);
TreeNode tn22 = new TreeNode(2);
TreeNode tn23 = new TreeNode(3);
tn21.left = tn22;
tn21.right = tn23;
tn22.left = null;
tn22.right = null;
tn23.left = null;
tn23.right = null; System.out.println(isSameTree1(tn11, tn21));
System.out.println(isSameTree2(tn11, tn21));
} @org.junit.Test
public void test2() {
TreeNode tn11 = new TreeNode(1);
TreeNode tn12 = new TreeNode(2);
tn11.left = tn12;
tn11.right = null;
tn12.left = null;
tn12.right = null; TreeNode tn21 = new TreeNode(1);
TreeNode tn22 = new TreeNode(2);
tn21.left = null;
tn21.right = tn22;
tn22.left = null;
tn22.right = null; System.out.println(isSameTree1(tn11, tn21));
System.out.println(isSameTree2(tn11, tn21));
} @org.junit.Test
public void test3() {
TreeNode tn11 = new TreeNode(1);
TreeNode tn12 = new TreeNode(2);
TreeNode tn13 = new TreeNode(1);
tn11.left = tn12;
tn11.right = tn13;
tn12.left = null;
tn12.right = null;
tn13.left = null;
tn13.right = null; TreeNode tn21 = new TreeNode(1);
TreeNode tn22 = new TreeNode(1);
TreeNode tn23 = new TreeNode(2);
tn21.left = tn22;
tn21.right = tn23;
tn22.left = null;
tn22.right = null;
tn23.left = null;
tn23.right = null; System.out.println(isSameTree1(tn11, tn21));
System.out.println(isSameTree2(tn11, tn21));
} public boolean isSameTree1(TreeNode p, TreeNode q) {
// p and q are both null
if (p == null && q == null) {
return true;
}
// one of p and q is null
if (q == null || p == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return isSameTree1(p.right, q.right) && isSameTree1(p.left, q.left);
} public boolean check(TreeNode p, TreeNode q) {
// p and q are null
if (p == null && q == null) {
return true;
}
// one of p and q is null
if (q == null || p == null) {
return false;
}
if (p.val != q.val) {
return false;
}
return true;
} public boolean isSameTree2(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (!check(p, q)) {
return false;
} // init deques
ArrayDeque<TreeNode> deqP = new ArrayDeque<TreeNode>();
ArrayDeque<TreeNode> deqQ = new ArrayDeque<TreeNode>();
deqP.addLast(p);
deqQ.addLast(q); while (!deqP.isEmpty()) {
p = deqP.removeFirst();
q = deqQ.removeFirst(); if (!check(p, q)) {
return false;
}
if (p != null) {
// in Java nulls are not allowed in Deque
if (!check(p.left, q.left)) {
return false;
}
if (p.left != null) {
deqP.addLast(p.left);
deqQ.addLast(q.left);
}
if (!check(p.right, q.right)) {
return false;
}
if (p.right != null) {
deqP.addLast(p.right);
deqQ.addLast(q.right);
}
}
}
return true;
}
}

LeetCode_100. Same Tree的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

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

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. js 异步执行顺序

    参考文章: js 异步执行顺序   1.js的执行顺序,先同步后异步 2.异步中任务队列的执行顺序: 先微任务microtask队列,再宏任务macrotask队列 3.调用Promise 中的res ...

  2. 云计算(9)--Gossip:multicast problem

    Gossip/Epidemic ptotocol 解决的问题是multicast problem Gossip 协议是电脑之间的通信协议,受启发与现实社会的流言蜚语.现代分布式系统通常用gossip协 ...

  3. Spring框架加案例

    依赖: <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependenc ...

  4. vim基本配置

    #set nocompatible # 打开语法高亮 syntax on # 在底部显示当前模式 set showmode # 命令模式下显示键入的指令 set showcmd # 支持使用鼠标 se ...

  5. 三十七.MySQL视图 MySQL存储过程

    1.视图的基本使用 把/etc/passwd文件的内容存储到db9库下的user表里 添加新字段id 存储记录的行号(在所有字段的前边) 创建视图v1 结构及数据user表的字段.记录一样. 创建视图 ...

  6. Cogs 750. 栅格网络(对偶图)

    栅格网络流 ★★☆ 输入文件:flowa.in 输出文件:flowa.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] Bob 觉得一般图的最大流问题太难了,他不知道如何解决, ...

  7. python去掉字符串中重复字符的方法

      If order does not matter, you can use   foo = "mppmt" "".join(set(foo)) set() ...

  8. CentOS 7 常用命令大全(转)

    博主最近疯狂迷恋上linux的centos 7 系统,特意从网上找了一篇centos 7的命令大全来学习,下面我分享下这个博客. 转载自:https://blog.csdn.net/o0darknes ...

  9. Spring boot 集成Solr

    首先安装Solr 集成 ikanalyzer ,可以参考 https://www.cnblogs.com/lick468/p/10867492.html https://www.cnblogs.com ...

  10. Nginx之web服务器

    Nginx的介绍 Nginx是由俄罗斯的Igor Sysoev使用C语言开发的轻量级.高性能.开源.跨平台的Web服务器. Nginx使用基于事件驱动的架构能够并发处理百万级的TCP连接,高模块化的设 ...