leetcode 100
100. Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
判断两个二叉树是否相同,结点的值和结构都相同。
采用递归来实现。
代码如下:
/**
* 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 && !q)
{
return true;
}
if(p && q && p->val == q->val)
{
if(isSameTree(p->left, q->left))
{
if(isSameTree(p->right, q->right))
{
return true;
}
}
}
return false;
}
};
leetcode 100的更多相关文章
- LeetCode 100. 相同的树(Same Tree) 2
100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...
- LeetCode 100. Same Tree (相同的树)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- LeetCode(100)题解--Same Tree
https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if th ...
- LeetCode 100 及 101题
100. 相同的树 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [ ...
- 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 相同树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- Java实现 LeetCode 100 相同的树
100. 相同的树 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [ ...
- leetcode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
随机推荐
- redis使用日志(一) 安装,调试
第一步:下载,安装 root@iZ28fgejjw7Z:/# wget http://download.redis.io/releases/redis-.tar.gz root@iZ28fgejjw7 ...
- 关于java MulticastSocket中的joinGroup(SocketAddress mcastAddr,NetworkInterface netif)
今天复习了一下java网络编程这方面(其实是之前没有学好),之前在linux下用c来做过一些例子,不过不好久没有用也就忘得一干二净了.不知道c/c++的东西不太好记,还是当初没好好学. 关于组播这方面 ...
- STM32中断控制及优先级设置
M3用8bits而STM32用高四位来表示抢占和子优先级:bit=1表示抢占:bit=0表示非抢占即子优先级:所以共有5中方案分组: 分组 Bit7 Bit6 Bit5 Bit4 说明: 第0组 ...
- JQuery之DataTables强大的表格解决方案
1.DataTables的默认配置 $(document).ready(function() { $('#example').dataTable(); } ); 示例:http://www.guoxk ...
- git config(转载)
From:http://www.g2w.me/2013/10/cache-github-credential-for-https-repository/ http://openwares.net/li ...
- 跨应用Session共享
摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...
- expdp impdp终极教学
源地址:http://blog.csdn.net/giianhui/article/details/7788550
- C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭
迄今为止,我们讨论的输入输出是以系统指定的标准设备(输入设备为键盘,输出设备为显示器)为对象的.在实际应用中,常以磁盘文件作为对象.即从磁盘文件读取数据,将数据输出到磁盘文件.磁盘是计算机的外部存储器 ...
- Delphi中自画TStatusBar面板中的文字颜色背景以及图片
转自 http://www.delphitop.com/html/kongjian/351.html 首先定义:procedure TStatusForm.FormCreate(Sender: TOb ...
- [SQL]获取所有数据库名、获取数据库中表名、获取表中的字段名
--()获取所有数据库名: Select Name FROM Master..SysDatabases order by Name --()获取所有表名 --XType=''U'':表示所有用户表; ...