LeetCode_100. Same Tree
100. Same Tree
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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 数据库中聚合索引(MySQL和SQL Server区别)
一.聚集索引和非聚集索引 聚集索引:类似字典的拼音目录.表中的数据按照聚集索引的规则来存储的.就像新华字典.整本字典是按照A-Z的顺序来排列.这也是一个表只能有一个聚集索引的原因.因为这个特点,具体索 ...
- 简单聊一下对MySQL索引的理解?
一.索引是什么? 索引是帮助MySQL高效获取数据的数据结构. 二.索引能干什么? 索引非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索引能够轻易将查询性能提高好几个数量级, ...
- span标签
<span>在行内定义一个区域,也就是一行内可以被<span>划分成好几个区域,从而实现某种特定效果.<span>本身没有任何属性. <span> 与& ...
- 通过 Ajax 调取后台接口将返回的 json 数据绑定在页面上
第一步: 编写基础的 html 框架内容,并引入 jquery: <!doctype html> <html lang="en"> <head> ...
- leetcode解题报告(15):Third Maximum Number
描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...
- Python3循环
Python中while语句的一般形式: while 判断条件: 语句 同样需要注意冒号和缩进,另外在Python中没有do…while循环 下面的实例计算1到100总和 ##calc.py n = ...
- vue文件中提示Expected Boolean, got String
这种情况是有一些属性的值应该填写Boolean类型,但是当前的值可能是“”--字符串 这种情况只需要在属性前面加上:即可. eg:
- 前端武器库系列之html后台管理页面布局
设计网页,让网页好看:网上找模板 搜 HTML模板 BootStrap 一.页面布局之主站页面 主站布局一般不占满页面,分为菜单栏.主页面.底部 上中下三部分.伪代码如下: <div class ...
- Linux 如何通过某一台服务器调用执行多台远程服务器上的脚本,结果显示在本地?
现在都流行自动化运维了,可能目前技术不够,很多自动化工具还不怎么会用,所以本次只是通过ssh来实现功能. 说明:自己写的一个简单脚本,只是实现了基础功能,还有待优化. 一共三台机器: master:1 ...
- [PKUSC2018]最大前缀和——状压DP
题目链接: [PKUSC2018]最大前缀和 设$f[S]$表示二进制状态为$S$的序列,任意前缀和都小于等于$0$的方案数. 设$g[S]$表示二进制状态为$S$的序列是整个序列的最大前缀和的方案数 ...