一. 题目描写叙述

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.

二. 题目分析

题目的意思非常easy,推断两棵树是否同样,递归,对两棵树的结点进行比較就可以。

三. 演示样例代码

#include <iostream>

using namespace std;

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 == NULL && q == NULL)
return true;
// p和q不同一时候到达叶节点,则剪枝
else if ((p != NULL && q == NULL) || (p == NULL && q != NULL))
return false;
return (p->val == q->val) && isSameTree(p->left, q->left)
&& isSameTree(p->right, q->right);
}
};

四. 小结

该题属于二叉树的遍历中的基础题目,难度不大。

最新文章

  1. RHEL6.4编译安装企业级LAMMP平台
  2. TEXshade教程- 多重比对着色软件包
  3. vs2010 “最近使用的项目”为空?解决办法!
  4. Pascal 语言中字符与字符串
  5. 如何在google test中指定只运行一部分测试
  6. StringHelper类,内容截取,特别适合资讯展示列表
  7. Javascript基础(2)
  8. java.lang.String类compareTo()返回值解析
  9. bzoj2395: [Balkan 2011]Timeismoney
  10. [Codeforces Round #192 (Div. 2)] D. Biridian Forest
  11. 使用svn与maven管理的项目导入Eclipse,但是与本地svn客户端关联不上?
  12. python中的递归函数
  13. win7 中如何设置eclipse的背景色--Console
  14. 2019 Power BI最Top50面试题,助你面试脱颖而出系列&lt;中&gt;
  15. Atitit s2018 s4 doc list dvchomepc dvccompc.docx&#160;.docx \s2018 s4 doc compc dtS44 \s2018 s4 doc dvcCompc dtS420 \s2018 s4f doc homepc \s2018 s4 doc compc dtS44\(5 封私信 _ 44 条消息)WebSocket 有没有可能取代 AJAX
  16. weblogic/tomcat Get乱码【转】
  17. 51nod1185 威佐夫游戏 V2 (模拟乘法)
  18. 为 Azure Resource Manager 中的虚拟机设置 WinRM 访问权限
  19. 使用Amanda ZRM备份远程MySQL数据库
  20. ASP.NET MVC:4 Ways To Prevent Duplicate Form Submission(转载)

热门文章

  1. MySQL服务器 IO 100%的案例分析
  2. 微信小程序official-account组件开发
  3. Android调用C#的WebService
  4. [LOJ#2980][THUSCH2017]大魔法师(线段树+矩阵)
  5. POJ1222 EXTENDED LIGHTS OUT 高斯消元 XOR方程组
  6. 【Codeforces528D】Fuzzy Search FFT
  7. Codeforces Round #280 (Div. 2) A. Vanya and Cubes 水题
  8. 电子助视仪 对比增强算法 二十种色彩模式(Electronic Video Magnifier, 20 color mode)
  9. IOS7开发~Images.xcassets
  10. WordPress 客户端软件列表