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. python-selenium安装笔记

    python-selenium操作火狐,谷歌,360,ie 亲测可用,windows环境 python3.6 由于python2到2020年不进行维护所有都用python3 selenium pip ...

  2. KaTex语法说明

    参考链接: https://katex.org/docs/supported.html https://github.com/KaTeX/KaTeX/blob/master/docs/supporte ...

  3. SecureCRT中解决乱码的问题

    SecureCRT中文乱码的问题,解决方法如下: 打开Option菜单,点击Session Options-     在Appearance外观这里,选择编码--UTF-8     一定要记得先保存! ...

  4. region特征

    一: 查看阈值之后的region特征,可以通过特征检测来看,在工具栏上 region特征分三部分: 1.基础特征: region面积,中心,宽高,左上角及右下角坐标,长半轴短半轴椭圆方向,洞数及其面积 ...

  5. Django REST framework+Vue 打造生鲜电商项目(笔记七)

    十.购物车.订单管理和支付功能 1.添加商品到购物车 (1)trade/serializer.py 这里的serializer不继承ModelSerializer,是因为自己写的Serializer更 ...

  6. ZOJ-3774 Power of Fibonacci——等比数列求和&&等价替换

    题目 求 $\displaystyle \sum_{i=1}^n F_i^k$,($1 \leq n\leq 10^{18},1 \leq  k\leq 10^5$),答案对 $10^9+9$ 取模. ...

  7. mysql查询重复数据

    SELECT * FROM oa_user ) ORDER BY UserName oa_user表名,UserName需要查重复的字段名

  8. [JLOI2013]卡牌游戏 概率DP

    [JLOI2013]卡牌游戏 概率DP 题面 \(dfs\)复杂度爆炸,考虑DP.发现决策时,我们只用关心当前玩家是从庄家数第几个玩家与当前抽到的牌是啥.于是设计状态\(f[i][j]\)表示有\(i ...

  9. Map集合类

    1.1 Map.clear方法——从Map集合中移除所有映射关系 public static void main(String[] args) {    Map map=new HashMap();  ...

  10. 宏任务、微任务与Event Loop

    说到宏任务和微任务,我们就不得不提 Event Loop 了 JS的本质是单线: 1. 一般来说,非阻塞性的任务采取同步的方式,直接在主线程的执行栈完成. 2. 一般来说,阻塞性的任务都会采用异步来执 ...