LeetCode 100. Same Tree (相同的树)
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Java Solution:
Runtime beats 23.17%
完成日期:07/01/2017
关键词:Tree
关键点:利用preOrder 来遍历tree;利用&&来控制左右两个children返回的都是true,才等于true。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public 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 false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
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相同的树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- 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判断树相同
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 ...
随机推荐
- [js高手之路]Node.js模板引擎教程-jade速学与实战3-mixin
强大的mixin mixin类似于函数的功能,可以达到模块复用的效果 mixin show: 定义一个类似函数的功能,名字叫show,里面的就是他的内容 +show: 调用show,每调用一次执行一次 ...
- delphi xe 3的EhLib 9.0 Build 9.0.033 Full Source安装
1.打开项目文件 2.全选 3.编译和buil 4.添加路径
- ACM退役记&&回忆录
ACM退役记 2017.9.19星期二,"九一八事变"八十六年后的第二天,永远记住这个日子,刚好是我报名ACM到现在,刚好满一年,而今天正是我注册杭州电子科技大学OJ的时间(就是这 ...
- markdown编辑器的学习
markdown编辑器的学习 1 标题 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 2列表 无序列表 1 2 3 4 有序列表 1 2 3 4 3引用 这里是引用,哈哈我也不知道到我引 ...
- 理解及操作环境变量(基于Mac操作)
通过本文,简单的了解下环境变量及其操作,与便于遇到相关问题时能够准确快捷的解决. 什么是环境变量 An environment variable is a dynamic-named value th ...
- 写了一个迷你confirm弹窗插件,有取消和确认操作处理并支持单个确认使用弹窗和锁屏禁止滚动
由于项目想精简不想用其他第三方的ui插件,又很需要像confirm等小效果来完善交互,且使用的频率也是相当的高,于是自己造了一个,省时也省力 代码已经粘贴出来,直接复制即可看到效果,高手勿喷,可以相互 ...
- Delphi中 StrToIntDef函数的用法
Delphi中 StrToIntDef函数的用法:比如我要判断一个文本框里输入的字符串能不能转换为integer类型,如果能,则返回转换后的整型数据,如果不能,则返回整数0,那么我就可以用strtoi ...
- CentOS 引导 Win10 启动项
因为无聊,所以想尝试一下双系统,所以在win10的基础之上,装了一个Linux系统,之前装过Ubuntu,几乎都是自动完成的无任何压力.但是想着Ubuntu好像更新换代有点快,所以换了个能用比较久的C ...
- 设计模式学习之“观察者模式” [C#]
<深入浅出设计模式>学习笔记第二章 需求: 开发一套气象监测应用,如图: 气象站,目前有三种装置,温度.湿度和气压感应装置. WeatherData对象追踪气象站的数据,并更新到布告板,布 ...
- .Net 内存对象分析
在生产环境中,通过运行日志我们会发现一些异常问题,此时,我们不能直接拿VS远程到服务器上调试,同时日志输出的信息无法百分百反映内存中对象的状态,比如说我们想查看进程中所有的Socket连接状态.服务路 ...