题目

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. 统计Apache或nginx日志里访问次数最多的前十个IP

    1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l 2.统计访问URL统计PV awk '{print $7}' access ...

  2. curry柯里化函数实现

    curry柯里化函数实现 参考文章: 一行写出javascript函数式编程中的curry 感谢作者分享 第一步: 缓存原始函数的参数个数 function curry(fn) { var limit ...

  3. 第03课 在VMwave 14.0 上配置企业级CentOS 6.6操作系统

    第一部分:配置虚拟硬件 1.1 启动VMware,选择文件-->新建虚拟机(Ctrl + N),创建一个虚拟机. (VMware的安装过程较为简单,可自行百度.) 1.2 此时,出现新建虚拟机向 ...

  4. Problem D: 勤奋的涟漪2 dp + 求导

    http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3 dp[i][state]表示处理了前i个,然后当前状态是state的时候的 ...

  5. solr 查询获取数量getCount()

    //前期设置好查询条件和参数 long numFound = 0; SolrQuery query = new SolrQuery("*:*"); query.setQuery(& ...

  6. python的与或非运算

    真的很重要,栽了个跟头!!!(虽然以前好像知道...) print(True or False and False) print((True or False) and False) # True # ...

  7. Web前端开发的四个阶段(小白必看)

    第一阶段:HTML的学习 超文本标记语言(HyperText Mark-up Language 简称HTML)是一个网页的骨架,无论是静态网页还是动态网页,最终返回到浏览器端的都是HTML代码,浏览器 ...

  8. JavaScript中,有三种常用的绑定事件的方法

    要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有三种常用的 ...

  9. checking for gcc... no

    ./configure 后显示checking for gcc... nochecking for cc... nochecking for cl.exe... noconfigure.sh:erro ...

  10. XML基本概念及增删改查操作

    一.概念及特征: 1. XML 指可扩展标记语言(Extensible Markup Language),用户可以自己定义标签.XML 被设计用来传输和存储数据,而 HTML 用于格式化并显示数据,并 ...