题目

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.

分析

判断两个二叉树是否相同。

采用递归的思想,当节点关键字以及左右子树均相同时,此两颗二叉树才相同;

AC代码

/**
* 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) {
//如果两个二叉树均为空,则返回true
if (!p && !q)
{
return true;
}
//如果两者其一为空树,则返回false
else if (!p || !q)
{
return false;
}
else{
if (p->val != q->val)
return false;
else
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
}
};

LeetCode(100) Same Tree的更多相关文章

  1. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  2. LeetCode(25)-symmetric tree

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  3. LeetCode(103) Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  4. LeetCode(124) Binary Tree Maximum Path Sum

    题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...

  5. LeetCode(26)-Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  6. LeetCode(102) Binary Tree Level Order Traversal

    题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...

  7. LeetCode(101)Symmetric Tree

    题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo ...

  8. LeetCode(1) Symmetric Tree

    从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...

  9. LeetCode(100):相同的树

    Easy! 题目描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 ...

随机推荐

  1. 递归(Recursion)

    递归是一种非常常用的算法,分为“递”和“归”两个步骤.满足递归算法有三个条件:1.一个问题,可以分解为子问题:2.该问题,与分解后的子问题,解决思路一致:3.存在终止条件.案例演示:假设有n个台阶,每 ...

  2. Centos 搭建Mysql-Proxy 读写分离

    Mysql 读写分离 主:192.168.153.130 从:193.168.153.131 Mysql-Proxy:193.168.153.132 这里省略mysql主从同步,如果有需要,请查看:M ...

  3. Centos6.8 搭建 Mysql 主从复制

    实例环境: MySQL-Master:Centos-6.8:192.168.153.130 MySQL-Slave:Centos-6.8:192.168.153.131 1.两台服务器安装mysql ...

  4. python之操作mysql(一)

    使用python操作mysql的思路: 1. 连接数据库:ip,端口号,密码,账号,数据库 2. 建立游标 3.执行sql语句 4.获取执行结果 5.关闭游标,关闭连接 conn = pymysql. ...

  5. [WOJ1583]向右看齐

    题目链接: WOJ1583 题目分析: 大水题--我就来水个题解 倒序扫,单调栈维护单减序列,每个对象的答案是栈里它下面那个元素 代码: #include<bits/stdc++.h> # ...

  6. Codeforces Round #408 (Div. 2) B

    Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, n ...

  7. JAVA字符串转日期或日期转字符串【转】

    JAVA字符串转日期或日期转字符串[转] 文章中,用的API是SimpleDateFormat,它是属于java.text.SimpleDateFormat,所以请记得import进 来! 用法: S ...

  8. Spark MLlib编程API入门系列之特征选择之向量选择(VectorSlicer)

    不多说,直接上干货! 特征选择里,常见的有:VectorSlicer(向量选择) RFormula(R模型公式) ChiSqSelector(卡方特征选择). VectorSlicer用于从原来的特征 ...

  9. php的iconv函数中utf8与utf-8的差异

    开发中遇到一个奇怪的问题:报错如下: iconv() [<a href='function.iconv'>function.iconv</a>] : Wrong charset ...

  10. Vue2.0实现路由

    Vue2.0和1.0实现路由的方法有差别,现在我用Vue 2.0实现路由跳转,话不多说,直接上代码 HTML代码 <div class="tab"> <route ...