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.

思路:如果两个树相同,那么他们的左子树、右子树必定也相同=>递归=>可用前序、中序或后续遍历。

以下用的是前序遍历,先处理根节点。

/**
* 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==NULL && q==NULL) return true;
else if(p!=NULL && q!=NULL)
{
if(p->val != q->val) return false;
}
else return false; if (!isSameTree(p->left,q->left)) return false; if (!isSameTree(p->right,q->right)) return false; return true;
}
};

100. Same Tree (Tree;DFS)的更多相关文章

  1. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  2. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  3. HUD5423 Rikka with Tree(DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 Rikka with Tree Time Limit: 2000/1000 MS (Java/O ...

  4. POJ3321/Apple tree/(DFS序+线段树)

    题目链接 Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9692 Accepted: 3217 Descr ...

  5. HDU 2489 Minimal Ratio Tree 最小生成树+DFS

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. poj3321-Apple Tree(DFS序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36442   Accepted: 10894 Desc ...

  7. CF1076E:Vasya and a Tree(DFS&差分)

    Vasya has a tree consisting of n n vertices with root in vertex 1 1 . At first all vertices has 0 0 ...

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

  9. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

随机推荐

  1. template.js 数据渲染引擎

    template.js 数据渲染引擎 template.js是一款JavaScript模板引擎,用来渲染页面的. 原理:提前将Html代码放进编写模板 <script id="tpl& ...

  2. 完全分布式hadoop2.5.0安装 VMware下虚拟机centos6.4安装1主两从hadoop

    请跟我走,从零开始搭建hadoop2.5.0环境.总览第一步:搭建三台能不用密码shh的虚拟机.第二步,装jdk,解压hadoop文件,配置环境变量和xml文件.第三步,复制克隆两个slave机器.调 ...

  3. CSS属性组-动画、转换、渐变

    一.动画 animation动画属性是一个简写属性,用于设置六个动画属性 aninmation-name动画名称,被调用 animation-duration完成动画需要的持续时间 animation ...

  4. day26-保护属性

    如果有一个对象,当需要对其进行修改属性时,有2种方法 1.对象名.属性名 = 数据 --->直接修改 2.对象名.方法名() --->间接修改 为了更好的保护属性安全,即不能随意修改,一般 ...

  5. MySql.Data.MySqlClient连接MySql

    在C#中连接MySql数据库其实是件很简单的事情,但对于刚开始学习C#的朋友来说,问题却是不小,主要原因是相对于ACCESS和MSSql来说,MySql方面的教程文章实在太少,我也是自己摸索好好半天才 ...

  6. 高德地图 API 计算两个城市之间的距离

    1. 目前在项目中,遇到一个需求不会做,就是要计算两个城市之间的距离,而这两个城市的输入是可变的,如果要使用数据库来先存储两地之间的距离,调用的时候再来调用,那么存数据的时候,要哭的,因为光是省级区域 ...

  7. css:在容器内文字超过容器范围,显示一行加省略号或者两行加省略号

    一.显示一行加省略号:各浏览器兼容 .box{ width: 100px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } ...

  8. 尚硅谷springboot学习4-helloworld探究

    1.POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  9. Linux Shell 内建命令:冒号(:)

    https://blog.csdn.net/honghuzhilangzixin/article/details/7073312/ 在Linux系统中,冒号(:)常用来做路径的分隔符(PATH),数据 ...

  10. DNS泛解析配置

    多个域名走同一个nginx代理服务器,多个域名如果有相同的后缀,就可以使用泛解析了,配置如下 编辑文件:/etc/dnsmasq.conf address=/aa.com/172.16.10.10 a ...