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.


 题目标题:Tree
  这道题目给了我们两个二叉树,让我们判断这两个二叉树是否一摸一样。利用preOrder 来遍历tree, 对于每一次判断,有三种可能性:
  1- 如果两个点都等于null -> 意思就是到达了最低端,并且它们是相等的,return true;
  2- 如果一个点等于null, 另外一个点不等于null, 那么它们是不相等的,return false;
  3- 如果两个点都不是null, 并且它们的值不相等,return false;
 
  剩下的情况就是它们两个点的值是相等的,把它们left 和 right children继续代入isSameTree function, 利用 && 来控制,一个点的left 和 right 返回的都是true,这个点返回的才是true。
 
 

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 (相同的树)的更多相关文章

  1. 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 ...

  2. [leetcode]100. Same Tree相同的树

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  3. LeetCode 100. Same Tree相同的树 (C++)

    题目: Given two binary trees, write a function to check if they are the same or not. Two binary trees ...

  4. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  5. [LeetCode] 100. Same Tree 相同树

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  6. [LeetCode]100. Same Tree判断树相同

    dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...

  7. 【LeetCode】Same Tree(相同的树)

    这道题是LeetCode里的第100道题. 这是题目: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 ...

  8. [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), ...

  9. LeetCode 101. Symmetric Tree (对称树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

随机推荐

  1. 编程从入门到提高,然后放弃再跑路(Java)

    1.Java入门篇 1.1 基础入门和面向对象 1.1.1 编程基础 [01] Java语言的基本认识 [02] 类和对象 [03] 类的结构和创建对象 [04] 包和访问权限修饰符 [05] 利用p ...

  2. Eclipse中删除tomcat server 导致不能重新创建该版本的tomcat server

    在Eclipse中创建了一个Web工程后,需要将该工程部署到Tomcat中进行发布.有时就会遇到在New Server对话框中选择了Tomcat 6/7后却无法单击“Next”按钮的问题,如下图所示: ...

  3. Linux环境下MySQL数据库用SQL语句插入中文显示 “问号或者乱码 ” 问题解决!

    问题: 在普通用户权限下执行 mysql -u root -p进入mysql数据库,中间步骤省略,插入数据:insert into 库名(属性)values('汉字'); 会出现如下提示:  Quer ...

  4. WEB中的敏感文件泄漏

    文件泄露, 根据泄漏的信息敏感程度, 在WEB漏洞中可以算是中危甚至高危的漏洞, 本篇文章就来 介绍下一些常见的泄漏, 主要分为由版本管理软件导致的泄露, 文件包含导致的泄露和配置错误导致的泄露. 版 ...

  5. ②jquery复习

    # jQuery 复习--by 传智前端与移动开发学院 ## 1. jQuery是什么?(了解)+ www.github.com+ jQuery 其实就是一堆的js函数,是普通的js,只不过应用广泛, ...

  6. 绘制QQ圆形图像

    思路:这里用到的是图像求交.现在画布上画上圆形,即所需要的圆形头像,然后设置paint的属性设置为求交集,再将bitmap划到canvas上面就好了. 代码如下: private static Bit ...

  7. java GUI编程一

    一.AWT介绍 所有的可以显示出来的图形元素都称为Component,Component代表了所有的可见的图形元素,Component里面有一种比较特殊的图形元素叫Container,Containe ...

  8. Highway Networks Pytorch

    导读 本文讨论了深层神经网络训练困难的原因以及如何使用Highway Networks去解决深层神经网络训练的困难,并且在pytorch上实现了Highway Networks. 一 .Highway ...

  9. 组合 Lucas定理

    组合 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u [Submit]   [Go Ba ...

  10. 一脸懵逼学习基于CentOs的Hadoop集群安装与配置

    1:Hadoop分布式计算平台是由Apache软件基金会开发的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS)和MapReduce(Google MapReduce的开源实现)为核心的 ...