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. Akka(25): Stream:对接外部系统-Integration

    在现实应用中akka-stream往往需要集成其它的外部系统形成完整的应用.这些外部系统可能是akka系列系统或者其它类型的系统.所以,akka-stream必须提供一些函数和方法来实现与各种不同类型 ...

  2. POJ 3190 Stall Reservations (优先队列)C++

    Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 271 ...

  3. Ant-打增量包

    如何打增量包具体用法见如下百度网盘的文档. http://pan.baidu.com/s/1gd5pAp1 1, ant 打源码增量包 build_incremental_src.xml 内容如下: ...

  4. 关于maven项目的一些报错问题

    本文为自己记录的笔记略显粗糙后续会把遇到的问题追加进来.忘大神多指点 1.父工程红色感叹号 此类问题解决方法,一般打开pom文件查看红色标记处是否有报错,也就是看哪个jar包没有下下来.分为两种情况. ...

  5. myeclipse一些快捷键 错了或者没说到补充下

    Ctrl + 1 快速修复Ctrl + D  删除当前行 Ctrl + Alt + ↓ 复制当前行到下一行(复制增加)Ctrl + Alt + ↑ 复制当前行到上一行(复制增加)Alt + ↓ 当前行 ...

  6. Zend Framework 3.0 安装及创建初始化项目教程

    前言: 最近开始接触关于PHP的框架的学习,然而PHP的框架少说也有七八种. 百度了一下,有人说ThinkPHP简单暴力的,有人说Laravel高大上的,等等等等,难以抉择. 最终我还是选择先从接触Z ...

  7. java基础解析系列(七)---ThreadLocal原理分析

    java基础解析系列(七)---ThreadLocal原理分析 目录 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系列(二)-- ...

  8. xcode7.3 iTunes Store operation failed解决

    使用apploader上传程序 提示:如果您安装了XCode开发环境.在/Applications/XCode.app/Contents/Applications目录中可以找到Application ...

  9. TTabControl

    1.TTabControl 组件的典型用法TTabControl 组件使用起来,根本不会使程序简单化,所以不提倡使用此组件,可以用TPageControl组件代替.与多页组件不同的是,虽然Tab 组件 ...

  10. Java比较器

    导语 本节内容,比较器Comparable是核心内容. 主要内容 重新认识Arrays类 两种比较器的使用 具体内容 Arrays类 在之前一直使用的"java.util.Arrays.so ...