【题目】

判断二叉树是否相同。

【思路】

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. Java中类似C#中Task.wait()的类CountDownLatch

    当主线程开辟多个子线程,而又需要这些子线程都执行完成后再执行主线程后续的操作,在C#中可以通过Task的wait方法来实现,然而在Java中也有类型的类CountDownLatch,具体用法如下: p ...

  2. jmap -histo pid 输出的[C [B [I [S 的含义

    JMAP 输出 其中: [C is a char[][S is a short[][I is a int[][B is a byte[][[I is a int[][]

  3. Arduino 开关控制小灯持续亮之具体思路

    Arduino 开关控制小灯持续亮之具体思路 为什么写这篇文章: 我们用开关控制灯的亮灭的时候,希望只需要按一下按键就可以做到灯一直亮着.而在<Arduino魔法书>中——有弹性的按键这一 ...

  4. spring cloud: zuul: 微网关-简单使用与路由配置

    spring cloud: zuul: 微网关-简单使用与路由配置 首先引入依赖 <dependency> <groupId>org.springframework.cloud ...

  5. change color3

    两种方法 第一种 DataGridview1.Rows[i].DefultCellStyle.backcolor 第二种 AlternatingRowsDefutCellstyle 属性 获取或设置应 ...

  6. 最简单的TTcpServer与TTcpClient通信实例-Delphi

    unit TcpSCDemo;//最简单的TTcpServer与TTcpClient通信实例-Delphi //Borland推出TTcpServer与TTcpClient作为主要的网络通信控件,意味 ...

  7. js滚动条如何缓慢的回到顶部?

    function top() { let currentPosition, timer timer = setInterval(function () { currentPosition = docu ...

  8. pytorch 中的 split

    Pytorch中的split问题: 1.使用torch.nn.Conv2d中有个参数是groups会将输入的feature map分组,此处需要注意的一点是分组之后各组的feature map的cha ...

  9. React Native之FlexBox布局

    参考原文链接:https://www.cnblogs.com/wujy/p/5841685.html 弹性盒模型(The Flexible Box Module),又叫Flexbox,意为“弹性布局” ...

  10. python爬虫基本原理及入门

    爬虫:请求目标网站并获得数据的程序 爬虫的基本步骤: 使用python自带的urllib库请求百度: import urllib.request response = urllib.request.u ...