【题目】

判断二叉树是否相同。

【思路】

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. v891 安卓平板的root

    看了 油管的视频,标题:Onda V891 - Root Android 4.4.4 (Kit Kat) 2)下载工具,照着做就行了. 3)root完之后,安装 [钛备份](TitaniumBacku ...

  2. 自动化部署之jenkins及简介

    一.什么是持续集成? (1)Continuous integration(CI) 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集 ...

  3. English trip V1 - B 17. Giving Information 提供信息 Teacher:Taylor Key: Person Information

    In this lesson you will learn to say your phone number and address.  这节课讲学习说你的手机号码和地址. 课上内容(Lesson) ...

  4. Vue.js示例:GitHub提交(watch数据,created钩子,filters过滤); 网格组件(功能:1.检索,2排序);

    GitHub提交 codePen:   https://codepen.io/chentianwei411/pen/wEVPZo 注意:频繁看案例,可能会被限制. 重点: 表单输入绑定, 单选按钮的使 ...

  5. 使用VisualStudio进行脚本|样式文件压缩

    在vs的Optimization中有个Bundle是专门用来压缩样式和脚本文件 .他有两个继承:StyleBundle.ScriptBundle,从名字上就可看出,StyleBundle专门压缩样式文 ...

  6. Spark分布式安装

    三台 服务器 n0,n2,n3 centos 6.4 X64 JDK, SCALA 2.11 Hadoop 2.2.0 spark-0.9.1-bin-hadoop2.tgz 说明: 1.所有机器上安 ...

  7. c++ shared_ptr的使用

    shared_ptr.是c++为了提高指针安全性而添加的智能指针,方便了内存管理.功能非常强大,非常强大,非常强大(不单单是shared_ptr,配合week_ptr以及enable_share_fr ...

  8. Fastjson, Gson, org.json.JSON三者对于JSONObject及JSONArray的判断

    1.Fastjson 我们通常在已知格式的情况下直接使用JSONObject,JSONArray,但是如果遇到需要判断格式呢? try{ Object object = JSON.parse(a); ...

  9. suffix array后缀数组

    倍增算法 基本定义子串:字符串 S 的子串 r[i..j],i≤j,表示 r 串中从 i 到 j 这一段也就是顺次排列 r[i],r[i+1],...,r[j]形成的字符串. 后缀:后缀是指从某个位置 ...

  10. IDA 逆向工程 反汇编使用

    IDA pro 7.0版本 from:freebuf 用到的工具有IDA pro 7.0  ,被反汇编的是百度云(BaiduNetdisk_5.6.1.2.exe). 首先,IDA pro的长相如下: ...