给定两个二叉树,写一个函数来检查它们是否相同。
如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的。
示例 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. FFmpeg滤镜代码级分析

    http://blog.chinaunix.net/uid-26000296-id-3322071.html 前一篇文章<为FFmpeg添加自定义滤镜>详细讲述了FFmpeg的滤镜添加步骤 ...

  2. 将PHP数组输出为HTML表格

    1. [代码][PHP]代码    <?phpclass xtable{    private $tit,$arr,$fons,$sextra;    public function __con ...

  3. 【转载】Myeclipse中实现js的提示

    近期需要大量使用JS来开发,但是MyEclipse2014自带的JS编辑器没有代码提示的功能,开发效率有点低,所以安装了一个Spket的插件,过程非常简单,SVN插件的安装比这个更简单. Spket插 ...

  4. Swing项目编译成exe,并且打包成安装文件(一)

    我们一般用java做Swing项目的时候一般都是只能在Myeclipse里面运行,那么怎么把我们的项目打包成exe可以直接双击运行呢? 初始工作:为了不让用户安装java环境,所以我们先新建一个文件夹 ...

  5. RequireJS 配置理解

    RequireJS 配置: 1.首先加载RequireJS文件 <script src="//cdn.bootcss.com/require.js/2.1.22/require.js& ...

  6. Android自动化测试怎么填写Xpath

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA5sAAAJYCAIAAABjAmXpAAAgAElEQVR4nOzdeZhb5Z0veNV/M88sz7 ...

  7. [SHOI 2007] 善意的投票

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1934 [算法] 首先 , 选择睡觉的人和不选择睡觉的人构成两个集合 这启发我们用最小 ...

  8. bzoj2330糖果——差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束,再建立一个源点0,向所有点连边权为1的边,表示每个人都会分到糖果: 答案较大 ...

  9. 解决最近windows版本Node.js中npm出现的“Error: ENOENT, stat 'C:\Users\UserName\AppData\Roaming\npm”的问题

    (转载请注明出处,from www.cnblogs.com/xdxer) 问题可能如下所示 解决方案: 在 'C:\Users\UserName\AppData\Roaming\‘ 下手动的增加一个文 ...

  10. HDU1711(KMP入门题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...