LeetCode之“树”:Symmetric Tree && Same Tree
Symmetric Tree
题目要求:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void updateLVec(TreeNode *left, vector<int>& lVec)
{
if(left)
{
lVec.push_back(left->val);
if(left->left || left->right)
{
if(left->left)
updateLVec(left->left, lVec);
else
lVec.push_back(INT_MIN);
if(left->right)
updateLVec(left->right, lVec);
else
lVec.push_back(INT_MIN);
}
}
} void updateRVec(TreeNode *right, vector<int>& rVec)
{
if(right)
{
rVec.push_back(right->val);
if(right->left || right->right)
{
if(right->right)
updateRVec(right->right, rVec);
else
rVec.push_back(INT_MIN);
if(right->left)
updateRVec(right->left, rVec);
else
rVec.push_back(INT_MIN);
}
}
} bool isSymmetric(TreeNode* root) {
if(!root || (!root->left && !root->right))
return true; if((!root->left && root->right) || (root->left && !root->right))
return false; vector<int> lVec, rVec;
updateLVec(root->left, lVec);
updateRVec(root->right, rVec); int szLVec = lVec.size();
int szRVec = rVec.size();
if(szLVec != szRVec)
return false; for(int i = ; i < szLVec; i++)
{
if(lVec[i] != rVec[i])
return false;
} return true;
}
};
虽然最终解决了问题,但代码还是复杂了。别人的代码(4ms)真叫一个简单呐:
bool isSymmetric(TreeNode *root)
{
if (!root) return true;
return isSymmetric(root->left, root->right);
}
bool isSymmetric(TreeNode *lt, TreeNode *rt)
{
if (!lt && !rt) return true;
if (lt && !rt || !lt && rt || lt->val != rt->val) return false;
return isSymmetric(lt->left, rt->right) && isSymmetric(lt->right, rt->left);
}
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.
有了上一道题的基础,这道题就很简单了,具体程序(0ms)如下:
/**
* Definition for a binary tree node.
* 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;
return isSameTreeSub(p, q);
} bool isSameTreeSub(TreeNode *lt, TreeNode *rt)
{
if(!lt && !rt)
return true;
if((!lt && rt) || (lt && !rt) || (lt->val != rt->val))
return false;
return isSameTree(lt->left, rt->left) && isSameTree(lt->right, rt->right);
}
};
LeetCode之“树”:Symmetric Tree && Same Tree的更多相关文章
- LeetCode之“树”:Balanced Binary Tree
题目链接 题目要求: Given a binary tree, determine if it is height-balanced. For this problem, a height-balan ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- LeetCode & tree & binary tree
LeetCode & tree & binary tree 树 & 二叉树 refs https://leetcode.com/problemset/all/?topicSlu ...
- 动态树之LCT(link-cut tree)讲解
动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树
[BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树 题目大意: 给定一个\(n(n\le50)\)个点,\(m(m\le1000 ...
- 【UOJ#388】【UNR#3】配对树(线段树,dsu on tree)
[UOJ#388][UNR#3]配对树(线段树,dsu on tree) 题面 UOJ 题解 考虑一个固定区间怎么计算答案,把这些点搞下来建树,然后\(dp\),不难发现一个点如果子树内能够匹配的话就 ...
随机推荐
- Scala:访问修饰符、运算符和循环
http://blog.csdn.net/pipisorry/article/details/52902234 Scala 访问修饰符 Scala 访问修饰符基本和Java的一样,分别有:privat ...
- OpenCV, MatBGR2ARGB, ARGB2MatBGR
代码片段~ unsigned int* abMatBGR2ARGB(Mat imag) { int nCols; int nRows; unsigned int *pbuff = NULL; if(i ...
- SpriteKit游戏开发 Challenge 2: An invincible zombie 问题的另一种解决方法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 该挑战的目的是僵尸碰到敌人时,将其设置为无敌模式,具体要求如下 ...
- 安卓2.x的版本使用4.x的主题
现在,还有大部分安卓开发者在开发安卓APP时使用的是2.x的SDK版本,为了兼容2.x的手机这本倒无可厚非,但最令人头痛的就是2.x版本的主题是在太丑了,这是安卓刚推出时只考虑到了实用,并没考虑到美观 ...
- Java基本语法-----java函数
函数的概述 发现不断进行加法运算,为了提高代码的复用性,就把该功能独立封装成一段独立的小程序,当下次需要执行加法运算的时候,就可以直接调用这个段小程序即可,那么这种封装形形式的具体表现形式则称作函数. ...
- css模块化及CSS Modules使用详解
什么是css模块化? 为了理解css模块化思想,我们首先了解下,什么是模块化,在百度百科上的解释是,在系统的结构中,模块是可组合.分解和更换的单元.模块化是一种处理复杂系统分解成为更好的可管理模块的方 ...
- 【移动开发】Context类bindService()参数
bindService()是Context的一个方法,它是抽象的.函数原型的代码如下:(android 2.3.3) /** * Connect to an application service, ...
- ExtJS学习(二)Ext组件模型
Ext中所有的组件都继承自Ext.component,这种单根继承的模型保证所有组件都拥有相同的通用方法与生命周期,这样在后续对这些组件进行维护管理时将更加便捷,同时也保证了在进行布局时的便利. 组件 ...
- ubuntu和mac OS X下另一种使用QQ的方法
在ubuntu可以到pidgin官网下载http://www.pidgin.im,然后再安装插件 pidgin-lwqq即可,步骤为: sudo add-apt-repository ppa:lain ...
- 开源项目——小Q聊天机器人V1.1
小Q聊天机器人V1.0 http://blog.csdn.net/baiyuliang2013/article/details/51386281 小Q聊天机器人V1.1 http://blog.csd ...