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.

Hide Tags

Tree Depth-first Search

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

Same Tree 深度优先的更多相关文章

  1. Symmetric Tree 深度优先搜索

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

  2. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  3. Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree)

    Leetcode之深度优先搜索(DFS)专题-559. N叉树的最大深度(Maximum Depth of N-ary Tree) 深度优先搜索的解题详细介绍,点击 给定一个 N 叉树,找到其最大深度 ...

  4. Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)

    Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...

  5. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  6. BFS广度优先 vs DFS深度优先 for Binary Tree

    https://www.geeksforgeeks.org/bfs-vs-dfs-binary-tree/ What are BFS and DFS for Binary Tree? A Tree i ...

  7. 257. Binary Tree Paths返回所有深度优先的遍历

    [抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  8. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. PAT甲级——A1023 Have Fun with Numbers

    Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...

  2. Hadoop Serialization hadoop序列化详解(最新版) (1)【java和hadoop序列化比较和writable接口】

    初学java的人肯定对java序列化记忆犹新.最开始很多人并不会一下子理解序列化的意义所在.这样子是因为很多人还是对java最底层的特性不是特别理解,当你经验丰富,对java理解更加深刻之后,你就会发 ...

  3. Leetcode589.N-ary Tree Preorder TraversalN叉树的前序遍历

    给定一个 N 叉树,返回其节点值的前序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...

  4. angular7.X配置同时使用localhost和本机IP访问项目

    1.项目中找到此文件“node_modules/webpack-dev-server/lib/Server.js”,按照下图修改: 二.修改配置文件package.json,见下图: 三.npm st ...

  5. 2017年8月9日学习内容存放 #socket通信介绍

    2017年8月9日学习内容存放 #socket通信介绍 ''' OSI七层 应用 表示 会话 传输 网络 ip 数据链路 mac 物理层 网线 http smtp dns ftp ssh snmp i ...

  6. JNI初级:android studio生成so文件详细过程

    本文主要参考blog:http://blog.csdn.net/jkan2001/article/details/54316375 下面是本人结合blog生成so包过程中遇到一些问题和解决方法 (1) ...

  7. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter[转]

    适配器模式的主要意图是对现有类的接口进行转换,以满足目标类的需求.其次,可以给目标类的接口添加新的行为(主要指方法).这一点容易与装饰模式混淆.从意图方面来看,装饰模式不改变(通常指增加)接口中的行为 ...

  8. Lab2 新增的细节

    entry.S 新增加了这个入口函数 bootloader 加载完成后 将执行 kern_entry 而非lab1 中的kern_init defs.h 使用了 ({})宏定义的方式,并且执行了一行定 ...

  9. python 全局种子与局部种子

  10. 阿里云杨敬宇:5G时代,边缘计算将发挥更大价值

    “5G时代,边缘计算将发挥更大价值.”3月8日,阿里云边缘计算技术负责人杨敬宇向媒体表示,边缘计算作为5G时代的一项关键技术,未来将成为不可或缺的基础设施之一. 5G时代万物智联将真正成为现实,但对计 ...