[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 ...
随机推荐
- alias重命名命令
升级了openssh后,发现ctrl+l忽然无法清屏了. 如果需要清屏的话,就得执行clear,但我更喜欢简单粗暴的做法,于是想起alias命令 方式一: 如果只想在当前终端生效(exit该窗口终端后 ...
- 菜鸟Vue学习笔记(二)
上一篇文章跟大家分享了Vue笔记上半部分,这篇文章接着跟大家分享.最近学习Vue越来越顺利了,今天接着学习,接着记录. 首先,来学习下常用的v-bind属性,它的作用是在属性中使用vue中定义的变量的 ...
- maven+dubbo+zookeeper 基础实例
1.maven 引入依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...
- spotlight工具监控oracle
spotlight工具版本Version: 5.0.1.1022 安装步骤 安装完成 安装之后,桌面上会生成如下图标 双机此图标,输入License 输入激活码 点击close,再次查看 建立连接,监 ...
- 函数和对象 及 prototype和__proto__
对象有 __proto__ 函数有 prototype 对象的__proto__指向构造自己的函数的prototype 但有一例外 var Obj = {v:99}var pObj = Object ...
- 作业-JSP简单入门
说明 本次作业不打分,仅作为大家自学的指导. 本次实验内容以"JSP实验参考文件"为主. 参考资料 Java教学问卷调查,有什么想说的,请尽情投票吧! 反射实验参考文件 JSP实验 ...
- base64图片内容下载转为图片保存
网页中的base64图片内容下载后,利用PIL转为图片保存 from skimage.io import imread from PIL import Image from cStringIO imp ...
- C++中宽字符类型(wchar_t)的编码
转载自: http://www.ituring.com.cn/article/111027 问题的起因是和一个朋友讨论不同编码的转换问题,说到了wchar_t的类型,朋友的看法是,wchar_t的编码 ...
- nodejs+mocha+supertest+chai进行测试(only demo)
1.nodejs安装成功 (上一篇:brew install nodejs) 2.mocha安装成功 npm install -g mocha 解释: -g代表global,全局的意思.此处mocha ...
- RobotFramework - AppiumLibrary 之元素定位
一.介绍 AppiumLibrary 是 Robot Framework 的App测试库. 它使用Appium 与Android 和 iOS应用程序进行通信,类似于Selenium WebDriver ...