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. Java-驼峰命名与下划线命名互转

    package com.xsh.util; /** * String工具类 * * @author xieshuang * @date 2019-05-23 */ public class Strin ...

  2. Jmeter 局域网的IP欺骗终极解决方案

    ip欺骗是什么?    ip欺骗就是模拟ip.什么意思呢,一个电脑就只有一个ip地址,当然如果有多块网卡的话,会有多个ip地址,一般服务器上有个网卡,咱们自己的电脑一般都只有一个ip地址,但是你做压测 ...

  3. error: ‘Poco::UInt16’ has not been declared

    碰到Poco库和其他第三方库共用的时候,当include-POCO库的头文件的时候,此时也include-其他库,导致这个报错. 原因是在这两个库中都对 UINT16 定义,导致冲突. 可以把这两个库 ...

  4. Elasticsearch Date类型,时间存储相关说明

    资料 网址 Elasticsearch 插入时间字段时数据格式问题 https://segmentfault.com/a/1190000016296983 Elasticsearch Date类型,时 ...

  5. Elasticsearch 索引文档如何使用自动生成 Id?

    一个文档的 _index . _type 和 _id 唯一标识一个文档. 我们可以提供自定义的 _id 值,或者让 index API 自动生成. 如果你的数据没有自然的 ID, Elasticsea ...

  6. Maven的下载以及配置

    Maven的下载以及配置 Maven的两大核心作用: (1)依赖管理:对Jar包的依赖,解决Jar包之间的冲突 (2)项目构建:项目从编译到测试到运行发布 一.Mavenu的下载(现在的eclipse ...

  7. CAN通信帧ID的含义解析? (转载)

    https://www.cnblogs.com/isAndyWu/p/10298695.html这个文章解答了我的一个id使用的疑惑,因此谢谢作者,转载. CAN总线ID是包含在报文帧中的. 1.主要 ...

  8. 使用cookie登录网盘账号

    ①使用Chrome浏览器登录百度网盘网页版 https://pan.baidu.com/ ②查看当前使用的cookie ③获取BDUSS 注意是全选复制,不要直接复制,会不全的. ④获取STOKEN ...

  9. IDEA+Maven+Mybatis 巨坑:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.UserMapper.findAll

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.User ...

  10. haproxy 2.0 dataplaneapi rest api 转为graphql docker 镜像

    为了方便直接使用haproxy dataplaneapi graphql 格式的查询,制作了一个简单的docker 镜像 基于dotenv 进行配置管理,可以直接通过环境变量传入参数,处理不同hapr ...