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++)的更多相关文章

  1. 100. Same Tree(leetcode)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  2. Device Tree(三):代码分析【转】

    转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...

  3. easyUI之Tree(树)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  4. 2021.07.19 BZOJ2654 tree(生成树)

    2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. C语言程序设计100例之(26):二进制数中1的个数

    例26   二进制数中1的个数 问题描述 如果一个正整数m表示成二进制,它的位数为n(不包含前导0),称它为一个n位二进制数.所有的n位二进制数中,1的总个数是多少呢? 例如,3位二进制数总共有4个, ...

  9. Device Tree(二):基本概念

    转自:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制是用 ...

随机推荐

  1. 2D游戏编程3—GDI

    WM_PAINT消息触发程序重新绘制界面,过程如下: PAINTSTRUCT    ps;    // used in WM_PAINT HDC        hdc;    // handle to ...

  2. WORD文档的长串数字如何粘贴到excel

    有问题,才有提高 问题描述: 现 word 文档中有好多长长的数字(如下),我需要将它们弄进 Excel 中 直接[复制],[粘贴],结果显示如下: 然后再设置单元格格式中的数字,无论选哪一个都得不到 ...

  3. HW4.16

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  4. yii项目开发项目常用技巧和方法汇总

    1.使用CActiveForm类组件如何输出不带html属性的结果 eg:<?php echo $form->textField($model,'email',array('size'=& ...

  5. 【转载】怎么理解Condition

    注:本文主要介绍了Condition和ReentrantLock工具实现独占锁的部分代码原理,Condition能在线程之间协调通信主要是AbstractQueuedSynchronizer和cond ...

  6. MongoDB 复制集 (三) 内部数据同步

    一 数据同步        一个健康的secondary在运行时,会选择一个离自己最近的,数据比自己新的节点进行数据同步.选定节点后,它会从这个节点拉取oplog同步日志,具体流程是这样的:      ...

  7. 【原创】javascript——prototype与__proto__

    一定要注意这个概念:javascript世界里,万物皆对象, function是对象,prototyp也是对象.   新建构造函数,并实例 var Person = function(){} var ...

  8. Conditionals with Omitted Operands (x ? : y)

    Conditionals with Omitted Operands The middle operand in a conditional expression may be omitted. Th ...

  9. ExtJS笔记--applyTo和renderTo的差别

    extjs中常常会用到renderTo或applyTo配置选项.这里,我就比較下两者的差别与使用方法.1.renderTo与render方法相应2.applyTo与applyToMarkup方法相应 ...

  10. Linux下设置最大文件打开数nofile及nr_open、file-max

    在开发运维的时候我们常常会遇到类似“Socket/File: Can’t open so many files”,“无法打开更多进程”,或是coredump过大等问题,这些都可以设置资源限制来解决.今 ...