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. 03.windows系统重新分配ip的cmd命令

    网络重启CMD命令 ipconfig /release  --- 释放ip搜索 ipconfig /renew --- 重新获得

  2. JS 时间函数 / 格式化时间戳

    处理时间主要使用时间对象 Date , 其提供两个静态方法 Date.now() //获得当前时间戳 Date.parse() //将字符串转化成时间戳 创建对象 new Date(); // 返回当 ...

  3. Django 模板格式化日期

    在模板中格式化日期: {{ post.date|date:”Y-m-d H:i:s” }}

  4. element el-upload组件获取文件名

    组件的连接:http://element-cn.eleme.io/#/zh-CN/component/upload 需求:点x按钮,获取文件名传到后端服务,把文件从服务器删除 分析: 仔细看文档,会发 ...

  5. delphi 控件编辑器

    控件编辑器和属性编辑器类似 http://www.rgzz.sdedu.net/ebook/hdbook/computer/bc/delphizhuanti/rmjq/028.htm TCommonD ...

  6. 根据svm将视频帧转换为img

    # -*- coding: utf-8 -*- """ Created on Mon Oct 1 09:32:37 2018 @author: Manuel " ...

  7. day04-完整性约束

    完整性约束 关键字: not null 与 default unique primary auto_increment foreign key 1.介绍 约束条件与数据类型的宽度一样,都是可选参数作用 ...

  8. js 选项卡制作

    知识回顾,制作JS选项卡,仅供参考 html代码: <!DOCTYPE html> <html lang="en"> <head> <me ...

  9. js中实现cookie的增删改查(document.cookie的使用详情)

    一.设置cookie的值 1.每个cookie都是一个名称/值对,名称/值对用等号连接,并将该名称/值对赋值给document.cookie即可.如:document.cookie="id= ...

  10. CSS GRID ESSENTIALS

    CSS GRID ESSENTIALS Review At this point, we've covered a great deal of different ways to manipulate ...