给定两个二叉树,写一个函数来检查它们是否相同。
如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的。
示例 1:
输入 :      1         1
             / \       / \
           2   3     2   3
        [1,2,3],   [1,2,3]
输出: true
示例 2:
输入  :    1          1
             /           \
           2             2
        [1,2],     [1,null,2]
输出: false
例 3:
输入 :     1          1
            / \         / \
           2   1     1   2
        [1,2,1],   [1,1,2]
输出: false

详见:https://leetcode.com/problems/same-tree/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null){
return true;
}else if(p==null||q==null){
return false;
}else if(p.val!=q.val){
return false;
}else{
return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
}

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. 100. Same Tree同样的树

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

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

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

  5. <LeetCode OJ> 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  6. 100. Same Tree(C++)

    100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...

  7. paip.tree 生成目录树到txt后的折叠查看

    paip.tree 生成目录树到txt后的折叠查看 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.ne ...

  8. 【BZOJ2588】Count On a Tree(主席树)

    [BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...

  9. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

随机推荐

  1. mysql初始化命令及其他命令

    这个问题纠结了我两年: 为了配置my.cnf中 undo的 参数生效,以及生成undo文件,使用一下命令 /usr/bin/mysql_install_db   --defaults-file=/et ...

  2. C语言中的排序算法--冒泡排序,选择排序,希尔排序

    冒泡排序(Bubble Sort,台湾译为:泡沫排序或气泡排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...

  3. android布局中使用include及需注意点

    在android布局中,使用include,将另一个xml文件引入,可作为布局的一部分,但在使用include时,需注意以下问题: 一.使用include引入 如现有标题栏布局block_header ...

  4. 关于RHEL5中yum挂载iso源引起的问题(转)

    今天在虚机上通过yum挂载iso源来安装rpm包,但提示错误,内容见下面.之前也有过这样的操作, mount后,修改repo文件,然后就可以yum install  rpm包了:过程很简单啊.不知道这 ...

  5. POJ1741:Tree

    浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:http://poj.org/problem?id=1741 这是一道树分治的模板题 ...

  6. bzoj2875随机数生成器——矩阵快速幂

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2875 矩阵快速幂,把x和c分开求,最后加上即可: 为防止爆long long,要用快速乘. ...

  7. Nwjs简单配置

    1.创建一个工程,配置一个  package.json 文件 { "name": "application-name", "version" ...

  8. live555 基本类之间的关系

    live555 中存在这5个最基本的类.每个类中都拥有一个BasicUsageEnvironment. 这是这几个类之间的相互关系.   MediaSession可以拥有多个subsession.

  9. .net网站部署时错误——未能加载文件或程序集(Could not load file or assembly)——的解决

    Could not load file or assembly 'System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKe ...

  10. 性能测试之Jmeter学习(一)

    一.Jmeter的安装: 1.安装配置要求: Java版本: Jmeter要求完全兼容的Java6或更高版本(建议安装java 8或以上版本): 操作系统:是一个100%的Java程序,它在任何支持完 ...