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. 神经网络原理及其c++实现

    1引言 数字识别是模式识别领域 中的一个重要分支,数字识别一般通过特征匹配及特征判别的传统方法进行处理.特征匹配通常适用于规范化的印刷体字符的识别,而 特征判别多用于手写字符识别,这些方法还处于探索阶 ...

  2. Java中重载和重写的区别

    重载 overloading 1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载是一个类中多态性的一种表现. 2) Java的方法重载,就 ...

  3. [ZETCODE]wxWidgets教程四:菜单栏和工具栏

    本教程原文链接:http://zetcode.com/gui/wxwidgets/menustoolbars/ 翻译:瓶哥 日期:2013年11月28日星期四 邮箱:414236069@qq.com ...

  4. HW4.27

    public class Solution { public static void main(String[] args) { int count = 0; for(int i = 2001; i ...

  5. Shell break和continue命令

    在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,像大多数编程语言一样,Shell也使用 break 和 continue 来跳出循环. break命令 break命令允许跳出所有循环(终止 ...

  6. PAT 1026. Table Tennis

    A table tennis club has N tables available to the public.  The tables are numbered from 1 to N.  For ...

  7. 一、两种方式初始化Mybatis

    一.xml Configuration.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTY ...

  8. SQL语法集锦一:显示每个类别最新更新的数据

    本文转载http://www.cnblogs.com/lxblog/archive/2012/09/28/2707504.html (1)显示每个类别最新更新的数据 在项目中经常遇到求每个类别最新显示 ...

  9. sql执行计划解析案例(二)

    sql执行计划解析案例(二)   今天是2013-10-09,本来以前自己在专注oracle sga中buffer cache 以及shared pool知识点的研究.但是在研究cache buffe ...

  10. Properties文件,Data,Calendar类的使用

    package cn.hncu.day9; import java.io.FileInputStream;import java.io.FileNotFoundException;import jav ...