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\),不难发现一个点如果子树内能够匹配的话就 ...
随机推荐
- Objective-C方法的实现
Objective-C的方法被两种数据类型描述:一个是选择子(SEL),它用来描述方法的名称;另一个是实现(IMP),它用来描述方法被调用时实际执行的代码(它们基本上只是C函数的指针). 类似于SEL ...
- Python logging 模块和使用经验
记录下常用的一些东西,每次用总是查文档有点小麻烦. py2.7 日志应该是生产应用的重要生命线,谁都不应该掉以轻心 有益原则 级别分离 日志系统通常有下面几种级别,看情况是使用 FATAL - 导致程 ...
- 【安卓开发】Layout Inflation不能这么用
Layout inflation在Android上下文环境下转换XML文件成View结构对象的时候需要用到. LayoutInflater这个对象在Android的SDK中很常见,但是你绝对没想到竟然 ...
- Android的SharedPreferences(首选项)保存键值对
使用共享首选项 如果您有想要保存的相对较小键值集合,您应使用 SharedPreferences API.SharedPreferences 对象指向包含键值对的文件并提供读写这些文件的简单方法. 每 ...
- Android必知必会-获取View坐标和长宽的时机
如果移动端访问不佳,请访问–>Github版 背景 最近要实现一个功能,用到了一些属性动画,需要获取一些View的坐标信息,设计图如下: 这里我使用的是DialogFragment来实现的,可以 ...
- 5.Qt自定义Button按钮的实现
1.编写自定义按钮 MyButton.h #ifndef MYBUTTON_H #define MYBUTTON_H #include <QWidget> /** * @brief ...
- 关于React Native 安卓首屏白屏优化
问题描述 在android中,当点击某个rn模块的入口按钮,弹出rn的activity到rn的页面展现出来的过程中,会有很明显的白屏现象,不同的机型不同(cpu好的白屏时间短),大概1s到2s的时间. ...
- Servlet之Request对象
下面的方法可用在 Servlet 程序中读取 HTTP 头.这些方法通过HttpServletRequest 对象可用. 1 Cookie[] getCookies() 返回一个数组,包含客户端 ...
- java操作properties配置文件
Java中有个类Properties(Java.util.Properties),主要用于读取Java的配置文件,将一些可能需要变化的值存放在properties中进行配置,通常为为.properti ...
- RecyclerView嵌套RecyclerView
ListView嵌套GridView http://blog.csdn.net/baiyuliang2013/article/details/42646289 RecyclerView下拉刷新上拉加载 ...