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. 分页器,序列化组件,bulk_create,choices字段

    分页器 <!--前端--> {% for book in page_queryset %} <p>{{ book.title }}</p> {% endfor %} ...

  2. StringUtils的isBlank()方法

    在校验一个String类型的变量是否为空时,通常存在3中情况 是否为 null 是否为 "" 是否为空字符串(引号中间有空格)  如: "     ". Str ...

  3. luogu 3380

    树状数组套权值线段树 #include <iostream> #include <cstdio> #include <algorithm> #include < ...

  4. Poj 2599 Godfather(树的重心)

    Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...

  5. CSPS模拟86-87

    模拟86 T1,烧水,按位统计贡献,利用某种sao操作避免数位dp #include<iostream> #include<cstdio> #include<cstrin ...

  6. 在vultr中安装k8s测试

    vultr 安装k8s *** 如果国内访问 k8s.gcr.io 很慢,或者无法访问 *** 在应用yaml文件创建资源时,将文件中镜像地址进行内容替换即可: 将k8s.gcr.io替换为 regi ...

  7. xshel链接linuxl安装nginx

    原文链接:https://blog.csdn.net/Sweet__dream/article/details/78256952?utm_source=blogxgwz9 这个连接更详细:https: ...

  8. 怎么写一个带 bin 的 npm 包

    只需要2步: 1. 在package.json 定义 一下 : { "name": "my-cli", ..., "bin": { &quo ...

  9. php rsa 非对称加解密类

    <?php header("Content-Type: text/html;charset=utf-8"); /* 生成公钥.私钥对,私钥加密的内容能通过公钥解密(反过来亦可 ...

  10. c++ 珊格画椭圆

    #ifndef _TEST_H #define _TEST_H #include <iostream> #include <math.h> using namespace st ...