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. SSH配置免秘钥登录

    一.  SSH 配置免秘要登录 配置SSH 免秘要登录,虽然就那么几步,但总是会出现点小问题,今天就做下记录.SSH 免秘钥就是让两台机器相互信任,不需要输入密码就能相互登录.配置相互信任就是把各自的 ...

  2. Eclipse: Android Device Chooser - Unknown Target

    公司最近所有的项目都使用到了Android开发手机(或PDA)应用.所需要的Android开发技术并不是非常复杂,因为我们的底层方法全部使用WebServcie写好了,做Android开发的人员只需要 ...

  3. JS和C# 里的闭包及闭包在事件中的使用

    在Javascript世界里,无所不用闭包及自定义事件, 自定义事件其实也是事先定义好一种规则 ,当触发者被响应后执行的一段回调.下面看个例子 function dothing(callBack){ ...

  4. urlencode rawurlencode htmlspecialchars htmlentities

    w string urlencode ( string $str ) 返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+).此 ...

  5. 如何在Pycharm设置ES6语法环境

    首先 如果不进行相关设置就刚ES6 语法的话,会出现下面提示性错误(运行还是能正常出效果的): (let 飘红, 这只是其中之一, 其他语法也会飘红) 接着,就是解决问题: 首先打开设置: 接着找到下 ...

  6. MySql 自适应哈希索引

    一.介绍 哈希(hash)是一种非常快的查找方法,一般情况下查找的时间复杂度为O(1).常用于连接(join)操作,如Oracle中的哈希连接(hash join). InnoDB存储引擎会监控对表上 ...

  7. Cocos2d-x3.0游戏实例之《别救我》第八篇——TiledMap实现关卡编辑器

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/musicvs/article/details/25368273 好吧.我真心全然搞不懂.我如今仅仅只 ...

  8. zabbix-2.4.8-1添加MySQL主从状态监控

    1.安装zabbix-agentyum -y install zabbix-2.4.8-1.el6.x86_64.rpm zabbix-agent-2.4.8-1.el6.x86_64.rpm 安装以 ...

  9. 剑指offer 面试56题

    面试56题: 题目:数组中数字出现的次数 题:一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 解题思路: 方法一:异或运算,详见<剑指offer&g ...

  10. gearman background后台job状态获取

    GearmanClient background job有一个方法叫: public array GearmanClient::jobStatus ( string $job_handle ) Get ...