[leetcode]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
题意:
给定两个树,判断两个数是否相同
Solution1: DFS (Code will be neat and simple)


code
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p ==null && q ==null) return true;
if(p ==null || q ==null) return false;
if(p.val == q.val){
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
return false;
}
}
Solution2: Iteration(Using a stack or a queue to simulate how DFS works)


code
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(p);
queue.add(q);
while(!queue.isEmpty()){
TreeNode node1 = queue.remove();
TreeNode node2 = queue.remove();
if(node1==null && node2 ==null) continue;
if(node1==null || node2==null) return false;
if(node1.val != node2.val) return false;
queue.add(node1.left);
queue.add(node2.left);
queue.add(node1.right);
queue.add(node2.right);
}
return true;
}
}
[leetcode]100. Same Tree相同的树的更多相关文章
- LeetCode 100. Same Tree (判断树是否完全相同)
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- LeetCode 100. Same Tree相同的树 (C++)
题目: Given two binary trees, write a function to check if they are the same or not. Two binary trees ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- [LeetCode] 100. Same Tree 相同树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- LeetCode 100. Same Tree (相同的树)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [LeetCode]100. Same Tree判断树相同
dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...
- 【LeetCode】Same Tree(相同的树)
这道题是LeetCode里的第100道题. 这是题目: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- Vue组件的介绍与使用
组件系统是将一个大型的界面切分成一个一个更小的可控单元. 组件是可复用的,可维护的. 组件具有强大的封装性,易于使用. 大型应用中,组件与组件之间交互是可以解耦操作的. 全局组件的使用 <!DO ...
- PHPSTORM ACTIVATION 注册激活
最近新出了PHPSTORM10,于是把自己机器上的升级了下.这家伙收费的,国人嘛...你懂的. 安装后,发现不能用原来的keygen注册激活了,于是Google了一下,下面是解决方案: 安装好打开的时 ...
- 第2章 Java基本语法(上): 变量与运算符
2-1 关键字与保留字 关键字(keyword) 保留字(reserved word) 2-2 标识符(Identifier) 案例 class Test{ public static void ma ...
- linux下C语言多线程编程实例
用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...
- linux-linnode满了的提示
线上有一台web服务器磁盘检测告警了,提示空间不足,登到服务器查看 <ignore_js_op> touch:cannot touch `furm.html': No space left ...
- MDX函数
MDX重点函数 成员函数 1..CurrentMember 获取运行时当前的成员,用法:<Dimension>.CurrentMember . 2..Parent 获取运行时当前的成员的父 ...
- 串口接收端verilog代码分析
串口接收端verilog代码分析 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////// ...
- Linux服务器调教日常
本文为Linux服务器调教日常,不保证正确. 1. sshd配置: https://www.cnblogs.com/byeyear/p/9289063.html 2. 禁止普通用户su 1. 编辑/e ...
- 笔记本 原来win10系统改装win7系统遇到 invaid signature detected.check secure boot policy setup问题
这次操作的笔记本电脑是 华硕R414U 大家如果遇到类似问题的话也可以参考这个方法,但是必须搞清楚电脑的型号,型号不同操作起来有差别的 我这里选择的重装系统的方法是最简单粗暴的硬盘安装方法,怎么硬 ...
- C语言中的作用域,链接属性和存储类型
作用域 当变量在程序的某个部分被声明的时候,他只有在程序的一定渔区才能被访问,编译器可以确认4种不同类型的作用域:文件作用域,函数作用域,代码块作用域和原型作用域 1.代码块作用域:位于一对花括号之间 ...