给定两个二叉树,写一个函数来检查它们是否相同。
如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的。
示例 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. "未预编译文件 因此不能请求该文件"问题处理

    手里一个项目重新编译后发布,访问时提示未预编译文件“default.aspx”, 因此不能请求该文件.综合网上的解决方法,做了如下操作: 1.重新安装了AJAX Extension: 2.项目添加引用 ...

  2. 纯属娱乐,对入门Android有一定的帮助

    package android.m9;   import android.app.Activity; import android.os.Bundle; import android.view.Men ...

  3. html5--3.10 input元素(9)

    html5--3.10 input元素(9) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 ...

  4. [SCOI 2014] 方伯伯的玉米田

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3594 [算法] 首先有一个结论 : 每次选择的区间右端点一定是n 根据这个结论 , ...

  5. Code-NFine:.NET快速开发平台 NFine.Framework Web框架

    ylbtech-Code-NFine:.NET快速开发平台 NFine.Framework Web框架 1.NFine.Framework 详细介绍返回顶部 1. NFine 是基于 C# 语言的极速 ...

  6. atom 的一些东东

    一. 配置atom atom 有些插件被墙了, 往往导致无法下载插件, 网上查了一些解决方案, 大部分就两种解决方案. 配置国内源 离线下载插件 1. 配置国内源 Linux 在 /home/user ...

  7. SyntaxError: can't assign to operator

    变量名不能有'-'

  8. Prime Independence

    题意: 对于给定集合,求解最大的子集合,使得集合内两两之商不为质数. 解法: 考虑对于每一个数字分解质因数可以得到 $O(nloglogNUM)$ 条两个数字不可以出现在同一集合的信息. 同时发现一条 ...

  9. XMLHttpRequest的用法

    转: 传统的Web应用请求服务器返回的一般是是完整的HTML页面,这样往往就需要页面进行刷新操作,不仅耗时而且用户体验度也不好.最典型的代表就是form表单登录操作了.如果登录失败往往是跳转到原网页重 ...

  10. UVaLive 3695 Distant Galaxy (扫描线)

    题意:给平面上的 n 个点,找出一个矩形,使得边界上包含尽量多的点. 析:如果暴力那么就是枚举上下边界,左右边界,还得统计个数,时间复杂度太高,所以我们考虑用扫描线来做,枚举上下边界, 然后用其他方法 ...