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:     1         1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true

Example 2:

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

Example 3:

Input:     1         1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false
---------------------------------------------------------------------------------------------
这个题就是判断两个二叉树是否相等。可以用DFS,也可以用BFS。
emmmmm,按理来说用DFS应该是简单的,至少代码好写,可是,我怎么感觉DFS比BFS还难???? C++代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p == NULL && q == NULL) return true;
if(p == NULL || q == NULL) return false;
if(p->val != q->val) return false;
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
};

(二叉树 递归 DFS) leetcode 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、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  3. (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree

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

  4. (二叉树 BFS DFS) 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 ...

  5. [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)

    描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...

  6. LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++

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

  7. Leetcode 100 Same Tree 二叉树

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

  8. [LeetCode] 100. Same Tree 相同树

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

  9. leetcode 100. Same Tree

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

随机推荐

  1. python ----面向对象的三大特性---多态

    多态 多态 不同的子类对象调用相同的父类方法,产生不同的执行结果. 以继承和重写父类方法为前提

  2. 为archlinux选择国内镜像

    pacman-mirrors --country China && pacman -Syyu

  3. Docker之进入容器(三)

    1.简介 经过前面两篇博客的扫盲,大家多多少少对docker有了一个基本的了解,也接触了docker的常用命令.在这篇博客中,我将介绍进入docker容器的几种方式. 2.进入docker中的几种方式 ...

  4. 完成代码将x插入到该顺序有序线性表中,要求该线性表依然有序

    #include <stdio.h> #include <malloc.h> int main(void) { int i, n; double s = 1.3; double ...

  5. 英语口语练习系列-C01-好棒

    古诗 来自百度 It's cool. It is neat. It's righteous! It's righteous! 酷毙了! righteous是 cool 的高级了 如果一件事让你无法用 ...

  6. Loj #2494. 「AHOI / HNOI2018」寻宝游戏

    Loj #2494. 「AHOI / HNOI2018」寻宝游戏 题目描述 某大学每年都会有一次 Mystery Hunt 的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得 ...

  7. day10-内置模块学习(一)

    今日份目录 1.模块之间的相互调用 2.代码结构的标准化 3.os模块 4.sys模块 5.collection模块 开始今日份总结 开始今日份总结 1.模块之间的相互调用 由于一些原因,总是会调用别 ...

  8. day 12 装饰器

    nonlocal关键字 # 作用:将 L 与 E(E中的名字需要提前定义) 的名字统一​# 应用场景:如果想在被嵌套的函数中修改外部函数变量(名字)的值​# 案例:​def outer():    n ...

  9. 23 python初学(模块和包)

    模块(module): 好处: 提高代码可维护性 + 编写代码不必从零开始 模块有三种: python标准库.第三方模块.应用程序自定义模块 另外,使用模块还可以避免函数名和变量名冲突,相同名字的函数 ...

  10. Python 输出文件内容到网络端口

    Python 输出文件内容到网络端口 $ cat mySocketTest.py import sys import time import socket if __name__ == "_ ...