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\),不难发现一个点如果子树内能够匹配的话就 ...
随机推荐
- 联想G510F1F2..功能键和FN+功能键反过来
进入BIOS, 将HotKey Mode 修改为Disabled,右边有详细说明:
- Dynamics CRM2013 从subgrid中打开快速创建窗体创建数据
在页面上使用subgrid时,在subgrid中新建数据时需要跳转到另一个页面,这种操作比较麻烦且很不友好,这时我们想到了快速创建窗体,像下图这样直接在当前页上方下拉出现一个窗体,填写内容后点击保存就 ...
- Android Stutio中使用java8的Lambda表达式
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51532028 本文出自:[openXu的博客] 目录: 为什么要使用Lambda表达式 让A ...
- 漏洞挖局利器-Fuzz技术介绍
模糊测试的定义 模糊测试定义为"通过向应用提供非预期的输入并监控输出中的异常来发现软件中的故障(faults)的方法". 典型而言,模糊测试利用自动化或是半自动化的方法重复地向应用 ...
- studio中碰到的jni问题:java.lang.UnsatisfiedLinkError
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52606328 studio中碰到 ...
- SQL Server 索引维护(1)——如何获取索引使用情况
前言: 在前面一文中,已经提到了三类常见的索引问题,那么问题来了,当系统出现这些问题时,该如何应对? 简单而言,需要分析现有系统的行为,然后针对性地对索引进行处理: 对于索引不足的情况:检查缺少索引的 ...
- Android简易实战教程--第二十一话《内容观察者监听数据库变化》
当数据库的数据发生改变,我们又想知道具体改变的情况时,就需要对数据库的变化情况做一个监控.这个任务,就由内容观察者来完成.下面这个案例,为短信数据库注册内容观察者,来监控短信的变化情况,当短信数据库发 ...
- 熟悉java语言的基本使用:简单存款取款机制java实现
最近一直没有项目做,于是我也不能这样闲着,我得开始学习新的技术,并且巩固以前自学的技术.以下就是我写的一个简单的java存取款代码,很简单,可能还有更简单的方法,目的是为了熟悉java的基本使用. p ...
- Java Socket输入流如何检测到EOF
对于InputStream的 read(b, off, len) 方法 public int read(byte[] b, int off, int len) throws IOException,J ...
- shell-----sed命令详解
Table of Contents 1. Sed简介 2. 定址 3. Sed命令 4. 选项 5. 元字符集 6. 实例 7. 脚本 1. Sed简介 sed是一种在线编辑器,它一次处理 ...