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. [js高手之路]Node.js模板引擎教程-jade速学与实战3-mixin

    强大的mixin mixin类似于函数的功能,可以达到模块复用的效果 mixin show: 定义一个类似函数的功能,名字叫show,里面的就是他的内容 +show: 调用show,每调用一次执行一次 ...

  2. java: Multiple encodings set for module chunk test "GBK" will be used by compiler

    IDEA 进行编译代码的时候,特别是新项目 特别容易出现 编码错误,但是 File-Encoding中设置的又没有问题,而且maven 是能打包的,就是用 idea 自带的 编译的时候 就会出现提示 ...

  3. python入门之一python安装及程序运行

    Python 程序要运行,需要先安装python解释器 PVM(这里可对照java的JVM来理解)实际上,你不需要单独安装,直接安装python后就可以了 1.安装python 下载地址:http:/ ...

  4. Spring 级联属性

    Spring 级联属性是当两个bean 关联时  从一个bean 给 另一个bean 赋值 Application xml  配置如下 <bean id="ZhangSan" ...

  5. String类的一些转换功能(6)

    1:把字符串转换成字节数组 getBytes() 如: String s = "你好啊!" //编码 byte [] arr = s.getBytes();//这里默认编码格式是g ...

  6. JAVA基础---编码解码

    所谓编码 即char->byte 所谓解码 即byte->char ISO-8859-1 中文字符会被黑洞吸收 全部变为"?" GB2312 汉字可以被编码为双字节 但 ...

  7. Instance of 的用法

    来自:百度百科 instanceof主要用于判断是否是某个类的实例 任何的对象都可以调用 返回结果是Boolean型数值Class AA a=new A();boolean b=a instanceo ...

  8. load data(sql)

    一般对于数据库表的插入操作,我们都会写程序执行插入sql,插入的数据少还可以,如果数据多了.执行效率上可能就不太理想了.load data语句用于高速地从一个文本文件中读取数据,装载到一个表中,相比于 ...

  9. JavaScript案例开发之扑克游戏

    随着时代的发展,知识也在日益更新,但是基础知识永远不会过时,它是新时代的基石,更是我们进一步学习的保障,下面带着大家用JavaScript开发一款真正的扑克游戏,和大家一起分享,希望你们能够喜欢:闲话 ...

  10. 80端口被system 占用

    1 运行'netstat -ano'发现80端口被pid=4的进程占用 2 打开任务管理器,发现pid=4的进程,其实是system进程,其对应的进程描述是NT kernel & system ...