Question

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.

Solution

递归判断,首先要判断结构是否一样,如果不一样直接返回false,如果结构一样,那么要继续判断值是否相等,如果不等直接返回false,如果相等,那么继续遍历左子树和右子树。

Code

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

LeetCode——same-tree的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  2. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  3. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  5. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  6. [LeetCode] Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  7. [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  8. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  9. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

随机推荐

  1. java中InputStream转化为byte[]数组

    //org.apache.commons.io.IOUtils.toByteArray已经有实现 String filePath = "D:\\aaa.txt"; in = new ...

  2. Jmeter中中文乱码

    jmeter-察看结果树-响应数据中的中文显示乱码 jmeter\bin\jmeter.properties 默认编码为:ISO-8859-1# The encoding to be used if ...

  3. 七牛上传ipa后自动生成plist文件

    1.利用模板技术动态生成plist文件的内容:(模板内容和data替换为plist需要的内容) //artTemplate <script src="js/template.js&qu ...

  4. Nginx nginx.conf配置文件详细说明

    在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data;    #启动进程,通常设置成和cpu的数量相等worker_process ...

  5. js 时间戳转换为指定的日期格式

      function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFul ...

  6. JavaScript Array 对象(length)方法 (contact、push,pop,join,map、reverse、slice、sort)

    一.Array对象属性 1.length 设置或返回数组中元素的数目. 数组的 length 属性总是比数组中定义的最后一个元素的下标大 1.对于那些具有连续元素,而且以元素 0 开始的常规数组而言, ...

  7. PLSQL 用的让自己更舒心点!

    每天开发时候  ,有时候会疲劳! 改下你的PLSQL 字体吧!

  8. 防sql注入之参数绑定 SQL Injection Attacks and Defense

    http://php.net/manual/zh/pdo.prepared-statements.php 预处理语句与存储过程 很多更成熟的数据库都支持预处理语句的概念.什么是预处理语句?可以把它看作 ...

  9. VMware虚拟机安装(二)

    我们一般用户使用的是windows ,那如何装一个Centos呢,我们一般学习的话,是搞一个VM虚拟机, 在虚拟机里安装一个Centos操作系统来学习,当然正式部署的话 我们一般是从阿里云或者西部数码 ...

  10. 设置请求timeout超时

    import requests r = requests.get("http://www.cnblogs.com/yoyoketang/", timeout=1) # 设置超时 p ...