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,这个机制是用 ...
随机推荐
- HDOJ/HDU 2087 剪花布条(indexOf()应用~~)
Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入 ...
- (转载)TRS内容管理平台用户注册逻辑漏洞
首先 site:gov.cn inurl:WCM TRS 的内容管理系统是国内政府网站使用最多的系统之一 如上面所说:外交部 http://wcm.fmprc.gov.cn/wcm/ 网址加上:wcm ...
- nyoj 123 士兵杀敌(四) 树状数组【单点查询+区间修改】
士兵杀敌(四) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战 ...
- hdoj 1068 Girls and Boys【匈牙利算法+最大独立集】
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- LigerUi中的Grid中不分页显示(local)!
LigerUi中的Grid中不分页显示! grid为local usePager: true, //是否分页
- 激活Navicat?如何注册Navicat?
在注册界面里面输入信息 名:顺便输入 组织:顺便输入 注册码:NAVH-WK6A-DMVK-DKW3
- 从struts2.1开始Convention零配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...
- sql执行计划解析案例(二)
sql执行计划解析案例(二) 今天是2013-10-09,本来以前自己在专注oracle sga中buffer cache 以及shared pool知识点的研究.但是在研究cache buffe ...
- 深入理解Fsync----JBD内核调试 专业打杂程序员 @github yy哥
http://hustcat.github.io/ http://www.cnblogs.com/hustcat/p/3283955.html http://blog.sina.com.cn/s/ar ...
- 模板类之间的友元关系实现Blob和BlobPtr
16.12编写你自己版本的Blob和BlobPtr模板,包含书中未定义的多个const成员. Blob.h(注意,成员函数的声明和定义要放在一个头文件中) /*记住,模板的头文件中通常既包括声明也包括 ...