100. Same Tree(C++)
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.
Subscribe to see which companies asked this question
题目大意:
判断两颗二叉树是否相等。
解题方法:
对比每一个节点是否相同。
注意事项:
节点相同的条件:值相等,左右孩子相同。
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==nullptr&&q==nullptr) return true;
else if(p==nullptr||q==nullptr) return false;
else return (p->val==q->val)&&isSameTree(p->right,q->right)&&isSameTree(p->left,q->left); }
};
100. Same Tree(C++)的更多相关文章
- 100. Same Tree(leetcode)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- Device Tree(三):代码分析【转】
转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...
- easyUI之Tree(树)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 2021.07.19 BZOJ2654 tree(生成树)
2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...
- 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 ...
- 572. Subtree of Another Tree(easy)
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)
题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...
- C语言程序设计100例之(26):二进制数中1的个数
例26 二进制数中1的个数 问题描述 如果一个正整数m表示成二进制,它的位数为n(不包含前导0),称它为一个n位二进制数.所有的n位二进制数中,1的总个数是多少呢? 例如,3位二进制数总共有4个, ...
- Device Tree(二):基本概念
转自:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制是用 ...
随机推荐
- NIR相机
近红外(NIR)相机——专为低照度环境而设计的高灵敏度相机 近红外光”(Near-Infrared) 是介于可见光和中红外光间的电磁波,因此是不能被人眼所察觉到的.近红外优化工业相机广泛适用于交通监控 ...
- Tornado源码探寻(准备阶段)
上一篇从一个简单的例子大致了解到Tornado框架的一个概述,同时也看清了web框架的本质. 接下来,我们从tornado程序的起始来分析其源码: 一.概述 上图是摘自朋友的博客里的内容,这张图很明确 ...
- shell脚本采用crontab定时备份数据库日志
测试服务器上才用定时脚本备份一个数据库 并打包压缩成tar避免文件过大 脚本如下: 测试服务器的shell backup_mysql.sh #!/bin/bash BASE_PATH=/alidata ...
- Guava库
Guava是一个非常棒的库,它就是Java标准库"所缺失的那部分",是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, ...
- "http-8080-3" java.lang.OutOfMemoryError: PermGen space C3P0死锁的问题
Exception in thread ""http-bio-8080"-exec-1" java.lang.OutOfMemoryError: PermGen ...
- 后台action处理数据传递给前台界面
package com.renmai.util; import java.io.IOException; import javax.servlet.http.HttpServletResponse; ...
- hdu 4773 Problem of Apollonius
莫名其妙就AC了-- 圆的反演-- 神马是反演? 快去恶补奥数-- #include<iostream> #include<map> #include<string> ...
- 深入了解当前ETL中用到的一些基本技术
数据集成是把不同来源.格式和特点的数据在逻辑上或物理上有机地集中,从而为企业提供全面的数据共享,是企业商务智能.数据仓库系统的重要组成部分.ETL是企业数据集成的概念出发,简要分析了当前ETL中用到的 ...
- UIwebView实现html的离线缓存
1.html的缓存主要採取ASIHTTPRequest的缓存策略 (1).设置缓存策略 //设置缓存 ASIDownloadCache *cache=[[ASIDownloadCache alloc] ...
- HTTP 404 - 未找到文件 怎么样解决
找不到网页 您要查看的网页可能已被删除.名称已被更改,或者临时不可用. -------------------------------------------------------------- ...