Same Tree

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

解法二:非递归

建立两个队列分别进行层次遍历,进队时检查对应点是否相等

/**
* 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(!isSameNode(p, q))
return false;
if(!p && !q)
return true; queue<TreeNode*> lqueue;
queue<TreeNode*> rqueue;
lqueue.push(p);
rqueue.push(q);
while(!lqueue.empty() && !rqueue.empty())
{
TreeNode* lfront = lqueue.front();
TreeNode* rfront = rqueue.front(); lqueue.pop();
rqueue.pop(); if(!isSameNode(lfront->left, rfront->left))
return false;
if(lfront->left && rfront->left)
{
lqueue.push(lfront->left);
rqueue.push(rfront->left);
} if(!isSameNode(lfront->right, rfront->right))
return false;
if(lfront->right && rfront->right)
{
lqueue.push(lfront->right);
rqueue.push(rfront->right);
}
}
return true;
}
bool isSameNode(TreeNode* p, TreeNode *q)
{
if(!p && !q)
return true;
if((p && !q) || (!p && q) || (p->val != q->val))
return false;
return true;
}
};

【LeetCode】100. Same Tree (2 solutions)的更多相关文章

  1. 【LeetCode】100. Same Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  2. 【LeetCode】100 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  3. 【LeetCode】101. Symmetric Tree (2 solutions)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  4. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  5. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  6. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  7. 【一天一道LeetCode】#100. Same Tree(100题大关)

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  8. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

随机推荐

  1. 实战-130W表增加字段耗时

    工作需要对130W的表增加字段,因为是操作线上数据库,所以提前在本地调查下耗时. 首先建表: CREATE TABLE `alter_cloumn_test` ( `id` int(11) unsig ...

  2. UML类图符号 各种关系说明以及举例(转载)

    文章出处:http://www.cnblogs.com/duanxz/archive/2012/06/13/2547801.html UML中描述对象和类之间相互关系的方式包括:依赖(Dependen ...

  3. nodejs 服务器 崩溃 2种解决办法

    用node启动server后,发现服务器不稳定,经常crash.我是用ssh远程登录的,ssh远程通道中断,或者Ctrl+C,都会使nodejs server崩溃掉. 一,node server 崩溃 ...

  4. Appium+python自动化18-brew、carthage和appium-doctor

    前言 本篇安装brew.carthage,解决启动appium时的报错问题,另外安装appium-doctor检查appium的环境 1.brew 2.carthage 3.appium-doctor ...

  5. 第十四章 openwrt 安装 python

    需要安装libffi,python-mini,python.libffi以及python-mini需要安装在python之前     如果部分软件包不一样可以在下面的web后台搜索,搜索前先opkg ...

  6. arm交叉编译opencv

    问题:undefined reference to `pthread_spin_init'解:修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthre ...

  7. matlab strel

    >>se3 = strel('square',3)Neighborhood: 1 1 1 1 1 1 1 1 1 >> se3 = strel('line',3 , 45)Ne ...

  8. [Android 源码] Android源码下载

    Android源码下载 为了能够顺利的下载Android的源码,同时也为了避免在网络上再次搜寻如何下载源码的麻烦,我把下载过程记录在这篇文档中. 官网中也有详细的介绍: http://source.a ...

  9. 几个有用的javascript(日期比较,数字验证,数字和汉字长度计算)

    1:日期大Js代码 //人员失效职位日期是否小于组织失效日期 function perDateInvalidate(){ var flag = true; //组织失效日期 var orgDate = ...

  10. TZOJ 1072: 编辑距离(动态规划)

    1072: 编辑距离 时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte 总提交: 917            測试通过:275 描写叙述 如果字符串的 ...