Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Example 1:

Input:
/ \ / \ [,,], [,,] Output: true

Example 2:

Input:
/ \ [,], [,null,] Output: false

Example 3:

Input:
/ \ / \ [,,], [,,] Output: false

方法一:使用递归(C++)

 bool isSameTree(TreeNode* p, TreeNode* q) {
if(p==NULL&&q==NULL)
return true;
if((!p&&q)||(p&&!q)||(p->val!=q->val))
return false;
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}

LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++的更多相关文章

  1. same tree(判断两颗二叉树是否相等)

    Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1 / \ 2 2 [1,2], [1,nul ...

  2. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...

  3. [LeetCode]100. Same Tree判断树相同

    dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...

  4. (二叉树 递归 DFS) leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  5. Leetcode 100 Same Tree 二叉树

    就是判断两棵树的值和结构是否相同 注意:要判断是否所有的树节点是否为NULL /** * Definition for a binary tree node. * struct TreeNode { ...

  6. 剑指offer17:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

    1 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 2 思路和方法 (1)先在A中找和B的根节点相同的结点 (2)找到之后遍历对应位置的其他结点, ...

  7. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. leetcode 100 Same Tree ----- java

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  9. Invert a binary tree 翻转一棵二叉树

    Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7 ...

随机推荐

  1. Python 虚拟环境[virtualenv/virtualenvwrapper]设置

      virtualenv virtualenv 是一个可以在同一计算机中隔离多个python版本的工具.有时,两个不同的项目可能需要不同版本的python,如 python2.6.6 / python ...

  2. 关于Mysql的主从

    mysql 主从同步异常原因及恢复:   1,auto.cnf 配置问题 这个问题是在部署主从复制的时候,可能会遇到 [1]报错 Last_IO_Error: Fatal error: The sla ...

  3. Python练习二

    1.计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和 sum1 = 0 sum2 = 0 count = 0 while count < 99: count += 1 if ...

  4. ubuntu 安装 pycharm

    添加源: $ sudo add-apt-repository ppa:mystic-mirage/pycharm   安装收费的专业版: $ sudo apt update $ sudo apt in ...

  5. 删除排序数组中的重复项-leetcode-26

    public:    int removeDuplicates(vector<int>& nums) {        int size=nums.size();        i ...

  6. Android项目——触摸按键控制LED

    一.Android Studio应用编程 1.应用程序界面layout对应的界面是activity_main.xml,后台对应的java文件是MainActivity.java,修改activity_ ...

  7. PHP之Trait详解

    php从以前到现在一直都是单继承的语言,无法同时从两个基类中继承属性和方法,为了解决这个问题,php出了Trait这个特性 用法:通过在类中使用use 关键字,声明要组合的Trait名称,具体的Tra ...

  8. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165308 张士洋

    2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165308 张士洋 1.进入官网下载Kali Linux VirtualBox版本的镜像文件. 2.解压并 ...

  9. 用 EPWA 写一个 图片播放器 PicturePlayer

    用 EPWA 写一个 图片播放器  PicturePlayer  . 有关 EPWA,见 <我发起并创立了一个 EPWA 的 开源项目>   https://www.cnblogs.com ...

  10. 使HTML5支持RTSP流 微信直播RTSP流 微信播放RTSP直播流(HTML5播放rtsp,web播放rtsp,微信支持rtsp)

    一.大家都知道HTML5的VIDEO可以播放视频,但是H5不支持RTSP播放,所以需要中间件! 二.我们经理长年的努力,开发了HTML5支持RTSP的中间件,使HTML5支持RTSP直播! 三.不卡顿 ...