【LeetCode】100. Same Tree (2 solutions)
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)的更多相关文章
- 【LeetCode】100. Same Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 【LeetCode】100 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【一天一道LeetCode】#100. Same Tree(100题大关)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
随机推荐
- Codeforces Round #304 (Div. 2) Break the Chocolate 水题
Break the Chocolate Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/546/ ...
- python 基本语法速览,快速入门
https://zhuanlan.zhihu.com/p/24536868 学习参考于这个博文. 我做一个笔记. 关于python一些常用的语法快速的预览,适合已经掌握一门编程语言的人.零基础,没有任 ...
- 人脸对齐SDM原理----Supervised Descent Method and its Applications to Face Alignment
最近组里研究了SDM算法在人脸对齐中的应用,是CMU的论文<Supervised Descent Method and its Applications to Face Alignment> ...
- TPS61040/61041 开关电源稳压器(DC-DC) ADJUST
Variable Control Voltage Output Voltage Adjust This method is accomplished by connecting a variable ...
- java.lang.RuntimeException: java.io.IOException: invalid constant type: 15
java.lang.RuntimeException: java.io.IOException: invalid constant type: 15 controller通过dubbo调用servic ...
- ExtJS遮罩层Ext.loadMask
一.可以直接应用在元素上,如: var loadMarsk = new Ext.LoadMask(target, { msg:'正在处理数据,请稍候......', removeMask:true / ...
- Struts2数据验证机制
1. 手动验证的实现 只需要在继承ActionSupport类的情况下,直接重写validate()方法即可.使用validate()方法可以对用户请求的多个Action方法进行验证,但其验证的逻辑是 ...
- 5 cocos2dx 3.0源码分析 渲染 render
渲染,感觉这个挺重要了,这里代入一个简单的例子 Sprite 建立及到最后的画在屏幕上, 我们描述一下这个渲染的流程: 1 sprite 初始化(纹理, 坐标,及当前元素的坐标大小信息) 2 主循 ...
- django 基础知识回顾
内容回顾: 1. ajax参数 url: type: data: 1.value不能是字典 {k1:'v1',k2:[1,2,3,],k3; JSON.string} 2.$('').serilize ...
- FreeBSD与Linux十个本质上的区别
Linux的标志是一只十分可爱的小企鹅,而FreeBSD的标志是一个拿着叉子的小恶魔.你是否经常会听到人们把 Linux及 BSD 系统混为一谈?是的,我有时会经常听到一些新手,甚至于媒体都这么说.当 ...