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. php多版本配置

    需求分析: 需要在一台装有php5.4的测试服务器跑的上php7.2.x的项目 安装phpenv(php版本控制) $ sudo yum install git $ mkdir -p repos/gi ...

  2. linux基础第四周

    天津SEO: 1.统计出/etc/passwd文件中默认shell为非/sbin/nologin的用户个数,并将用户都显示出来 [root@localhost ~]# awk -F: -v i=&qu ...

  3. 使用unittest测试(基础一)

    #导入unittest单元测试框架 ##用例的方法前缀必须要以 test 开头的 #这是用来组织用例的 import unittest class TestDBQB(unittest.TestCase ...

  4. 题解 [51nod1753] 相似子串

    题解 [51nod1753] 相似子串 题面 解析 先考虑相等的时候怎么办, 我们考虑求出每个字母的贡献,这样字母相等的问题就可以用并查集来解决. 具体来说,我们先对于每个字母,把S中等于它的标为1, ...

  5. window.external 是调用外部方法

    ie中,window.external 是调用外部方法,比如,是在 winform 中的 webbrower 中使用 window.external.SendData(),那么,SendData()  ...

  6. 081_使用 awk 编写的 wc 程序

    #!/bin/bash#自定义变量 chars 变量存储字符个数,自定义变量 words 变量存储单词个数#awk 内置变量 NR 存储行数#length()为 awk 内置函数,用来统计每行的字符数 ...

  7. jQuery相关方法2

    一.元素样式设置的方式(css,json键值对,链式编程) <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js ...

  8. leetcode解题报告(18):Contains Duplicate

    描述 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  9. NCNN使用总结

    目录 NCNN简介 NCNN注意事项 NCNN使用心得 小技巧 小想法 NCNN简介 ncnn 是一个为手机端极致优化的高性能神经网络前向计算框架.ncnn 从设计之初深刻考虑手机端的部署和使用.无第 ...

  10. CMD browser in Linux -- Links

    Links is an open source web browser written in C programming Language. It is available for all major ...