(二叉树 递归 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 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的更多相关文章
- 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 ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- (二叉树 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 ...
- (二叉树 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 ...
- [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
- 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 ...
- Leetcode 100 Same Tree 二叉树
就是判断两棵树的值和结构是否相同 注意:要判断是否所有的树节点是否为NULL /** * Definition for a binary tree node. * struct TreeNode { ...
- [LeetCode] 100. Same Tree 相同树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
随机推荐
- 微信小程序基本目录结构学习
今天我们就以firstdemo为例,介绍一下小程序的基本目录结构.当我们打开一个微信小程序项目后,点击进入“编辑”菜单,我们可以看到有以下5个文件/文件夹):pages文件夹,utils文件夹,全局文 ...
- Ubuntu 18.04 安装博通(Broadcom)无线网卡驱动
目录 Ubuntu 18.04 安装博通(Broadcom)无线网卡驱动 Package gcc is not configured yet. 解决办法 history history | grep ...
- 小学生四则运算(java编程)201571030135
任务1源码在Github的仓库主页链接地址: https://github.com/zhanghh2018/Four-primary-school-pupils 需求分析: 作业总体效果:随机产生n道 ...
- java实现Windows记事本
给大家分享下我之前的作品: 源文件下载: 链接:https://pan.baidu.com/s/1N45VsS9aVgmvhvYjRLxBrA 提取码:b9fg 源码: JF_Notpad.jav ...
- Redis(十一):Redis的事务功能详解
相关命令 1. MULTI 用于标记事务块的开始.Redis会将后续的命令逐个放入队列中,然后才能使用EXEC命令原子化地执行这个命令序列. 这个命令的运行格式如下所示: MULTI 这个命令的返回值 ...
- linux 网络套接字
在内核分析网络分组时,底层协议的数据将传输到跟高的层.而发送数据的时候顺序是相反的.每一层都是通过加(首部+净荷)传向跟底层,直至最终发送. 这些操作决定了网络的的性能. 就如下图所示 linux因此 ...
- Windows的GDI映射方式,逻辑坐标,设备坐标的理解
最近在学Win32的编程,看的是<Windows程序设计第5版>一书,这本书是台湾人翻译的,有些译法和大陆不一样,书中还有一些错误的地方,很多时候需要中英文对照阅读,下载请点击 https ...
- RabbitMQ持久化
我们知道,如果消息接收端挂了,消息会保存在队列里.下次接收端启动就会接收到消息. 如果RabbitMQ挂了怎么办呢?这时候需要将消息持久化到硬盘 消息发送端:producer ........... ...
- .NET IL实现对象深拷贝
对于深拷贝,通常的方法是将对象进行序列化,然后再反序化成为另一个对象.例如在stackoverflow上有这样的解决办法:https://stackoverflow.com/questions/785 ...
- Linux内存管理 (1)物理内存初始化
专题:Linux内存管理专题 关键词:用户内核空间划分.Node/Zone/Page.memblock.PGD/PUD/PMD/PTE.lowmem/highmem.ZONE_DMA/ZONE_NOR ...