100. Same Tree

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
/**
* 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 (p == q);
} return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};

LeetCode 100. Same Tree (判断树是否完全相同)的更多相关文章

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

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

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

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

  3. 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 ...

  4. leetcode 100. Same Tree、101. Symmetric Tree

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

  5. LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

      思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...

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

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

  7. LeetCode 101. Symmetric Tree 判断对称树 C++

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

  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相同的树 (C++)

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

随机推荐

  1. Win10 Microsoft Store 微软商店 Error 0x00000193 解决方法

    0x00 前言 最近使用 CFW 过程中使用 Fiddle Web Debug 设置 Store 的回环代理的过程中发现无论是否使用代理,Store 都无法访问网络的问题,在最下面的提示中出现了 0x ...

  2. git远程上的分支到本地

    先想一个自己要在本地新建的分支名称,qianjinyan git checkout -b qianjinyan origin/SELLER-2248-1018 git branch 查看分支 git ...

  3. 离线安装python的ibm_db模块

    目前手头的项目是一个UI自动化框架,其中有些模块的功能需要与DB2数据库交互,于是百度了一下python操作DB2的模块是 ibm_db. 然而我的工作机器是一台windows堡垒机,不能联网,固不能 ...

  4. Linux的sz和rz命令

    工作中需要在Linux和Windows之间传输文件时,一般使用winscp或者ftp工具来完成,最近才知道有sz和rz这两个命令,方便好用. sz  下载 从Linux下载文件到本机 , 在Linux ...

  5. urdf 学习记录

    1.URDF(Unified Robot Description Format),统一的机器人描述文件格式.主要用来描述机器人的几何形状,在可视化时(如RViz中)显示出机器人的几何形状.与画图软件( ...

  6. 闲谈关于discuz内核缓存机制

    Discuz! 缓存 Discuz! X2.5 的 config_global.php 中有这样一行代码 $_config['cache']['type'] = 'sql'; 这就是 Discuz! ...

  7. hdfs的文件个数 HDFS Quotas Guide

    HDFS Quotas Guide Overview HDFS允许管理员为多个每个目录设置使用的命名空间和空间的配额.命名空间配额和空间配额独立操作,但是这两种类型的配额的管理和实现非常类似. Nam ...

  8. modbus-poll和modbus-slave工具的学习使用——modbus协议功能码3的解析(及欧姆龙温控器调试笔记)

    最近的项目中使用到了欧姆龙的温控器,里面有很多的通信方式,我们使用的常见的modbus——RTU方式,其他方式我们不使用,其中通信手册上面有很多通信的实例,欧姆龙modbus还区分4字节模式和2字节模 ...

  9. java语言评价--java帝国

    “陛下您想想,我们有很多宝贝,” IO大臣根本不理线程大臣, 继续侃侃而谈:“ 比如IoC, AOP,反射.动态代理.泛型.注解.JDBC.JMS...... 还有我们引以为豪的JVM.这些东西,那些 ...

  10. ajax请求数据时,get和post的区别

    发送机制 1.get请求会将参数跟在URL后面进行参数传递,而post请求则是作为http消息的实体内容发送给web服务器: 2.get提交的数据限制是1024字节,这种显示是来自特定浏览器和服务器对 ...