【题目】

判断二叉树是否相同。

【思路】

check函数。

p==null并且q==null,返回true;(两边完全匹配)

p==null或q==null,返回false;(p、q其中一方更短)

p.val==q.val,值相同,继续迭代向左向右遍历check(p.left,q.left)&&check(p.right,q.right);

【代码】

    public boolean check(TreeNode p, TreeNode q){
if(p==null&&q==null)
return true;
if(p==null||q==null)
return false;
if(p.val==q.val)
return check(p.left,q.left)&&check(p.right,q.right);
return false;
}

[Leetcode 100]判断二叉树相同 Same Tree的更多相关文章

  1. [Leetcode 101]判断对称树 Symmetric Tree

    [题目] Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  2. LeetCode 100. 相同的树(Same Tree) 2

    100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...

  3. LeetCode 101. 对称二叉树(Symmetric Tree)

    题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null, ...

  4. leetcode 98,判断二叉树为BST

    方法一,记录子树的上界和下界,root的左子树一定小于root的值,root的右子树一定大于root的值,然后递归左子树和右子树 public class Solution { public bool ...

  5. 递归 - Leetcode 110 判断二叉树是否为平衡二叉树

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

  8. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. Python量化库大全

    https://zhuanlan.zhihu.com/p/26983703?utm_source=wechat_session&utm_medium=social 这个网址上详细介绍了,做量化 ...

  2. Linux awk命令详解 + 练习

    https://www.cnblogs.com/ftl1012/p/9250541.html 练习步骤: 1.我先是在root文件下面创建一个yan.txt文件,然后在文件中随便敲了几个字符串,由空格 ...

  3. Fat jar用途

    1.Fat jar用途 我们都知道默认的eclipse下打包jar时,是无法连同外部引用包一起打包的.所以一般我们可以自己编写MINIFEST.MF文件来手动将需要的jar包加入进来,但是这样对于引用 ...

  4. Redisson实现分布式锁(二)

    本次基于注解+AOP实现分布式锁(招式与前文基于注解切换多数据源相同),话不多说,直接上样例: 首先自定义注解:设计时需要考虑锁的一般属性:keys,最大等待时间,超时时间,时间单位. package ...

  5. RLE Plots: relative log expression

    RLE Plots: Visualising Unwanted Variation in High Dimensional Data 参考:RLE Plots: Visualising Unwante ...

  6. blast | diamond 输出结果选择和解析 | 比对

    之前的文章:构建NCBI本地BLAST数据库 (NR NT等) | blastx/diamond使用方法 | blast构建索引 | makeblastdb 本地运行blast时,需要指定out fo ...

  7. Django 的 orm 查询

    一.模型关系表 1. 一对一 Author-AuthorDetail 关联字段可以在任意表下,但必须唯一约束.(unique约束) ad_id(unique约束) ad = models.oneToO ...

  8. 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)

    题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...

  9. python:字典嵌套列表

    Python的字典{ }以键值对的形式保存数据,可以以键来访问字典中保存的值而不能用下标访问.字典中几乎可以包含任意的变量,字典,数列,元组.数列也一样. python的列表[ ]与字典不同,列表通过 ...

  10. 廖雪峰网站:学习python函数—调用函数(一)

    # 调用函数 # 可以直接从Python的官方网站查看文档: # http://docs.python.org/3/library/functions.html#abs n = abs(100) # ...