给定两个二叉树,写一个函数来检查它们是否相同。
如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的。
示例 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. RBAC打造通用web管理权限

    https://www.cnblogs.com/lamp01/p/6576432.html https://www.imooc.com/learn/799

  2. VS2010关于调用ffmpeg借口出错

    win7 下开发视频服务器,用到ffmpeg,debug版本运行正常,切换到release时,出现"0x00905a4d 处未处理的异常: 0xC0000005: 读取位置 0x00905a ...

  3. Git学习笔记 - Git安装

    Git安装(Windows) 从 https://git-for-windows.github.io/ 下载Git,下载完成,双击安装,一路选择默认设置即可. 注意:选择使用git的命令行模式,选择默 ...

  4. JavaWeb学习总结(三)response与request

    一.response response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletResponse.在客户端发出每个请求时,服务器 ...

  5. JQ里的this与$(this)

    网上有很多关于jQuery的this和$(this)的介绍,大多数只是理清了this和$(this)的指向,其实它是有应用场所的,不能一概而论在jQuery调用成员函数时,this就是指向dom对象. ...

  6. Win10+CUDA9.0+cudnn7.1安装

    CUDA下载 cudnn下载 CUDA默认安装即可. cudnn下载解压之后,将对应的文件分别拷贝到CUDA  Toolkit中即可: 对应的文件夹为: 若为默认安装,则应分别拷贝到的文件夹如下: C ...

  7. Spring中JdbcTemplate的基础用法

    Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...

  8. CF-799A

    A. Carrot Cakes time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  9. ** Error in `./g2o_viewer': realloc(): invalid pointer:

    问题: defe@defe-Precision-Tower-3620:~/project/Demo/UseG2OforPoseGraph/useg2oforposegraph$ ./g2o_viewe ...

  10. 解决 'chromedriver' executable needs to be in PATH.'报错

    试了把chromedriver.exe放到chrome安装文件下,python安装文件下,然后把路径配到path里,均无用. 最后是修改函数调用得以解决: from selenium import w ...