[leetcode] 10. 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 3But the following is not:
1
/ \
2 2
\ \
3 3Note:
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.OJ's Binary Tree Serialization:
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
\
5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
.
我之前的思路有问题,我是想先做一个函数然后记录到某个子叶的路径,这个路径拿左右来表示,0表示左,1表示右。结果这个函数的递归的版本我没写出来,因为结束弹出方法我实在没找到,然后迭代我觉得实在太烦了。然后就想着换另一种方法:把整个函数的出口另外做一个简单的递归,然后在主函数里面调用一次就行。
题解如下:
/**
* 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 Symmetric(TreeNode *left, TreeNode *right)
{
if (left == NULL && right == NULL)
{
return true;
}
if (left == NULL || right == NULL)
{
return false;
}
return left->val == right->val && Symmetric(left->left, right->right) && Symmetric(left->right, right->left);
} bool isSymmetric(TreeNode *root)
{
return root != NULL ? Symmetric(root->left, root->right) : true;
}
};
即如上。之后leetcode的题目好像要买它的书之后才能再刷了,不过那是对应的是新出的题目,老的题目还是可以的,先把之前的题目刷完再说吧。
[leetcode] 10. Symmetric Tree的更多相关文章
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode 题解]: Symmetric Tree
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- Bootstrap学习之路(1)---开篇-登陆页
Bootstrap是现在很流行的一套前端框架,尤其是它的自适应,真的很不错,而且对移动设备也很友好,可以达到快速开发的效果,最近想把自己的网站弄个手机版,很果断的就选用了bootstrap,跟大家分享 ...
- python使用scikit-learn计算TF-IDF
1 Scikit-learn下载安装 1.1 简介 1.2 安装软件 2 TF-IDF基础知识 2.1 TF-IDF概念 2.2 举例说明计算 3 Scikit-Learn中计算TF-IDF 3.1 ...
- Hive-表连接
Hive只支持等值连接,即ON子句中使用等号连接,不支持非等值连接. Hive内置的数据存储类型,TextFile, SequenceFile, ORC(列式存储) 如果连接语句中有WHERE子句,会 ...
- flask 蓝图
转自:http://spacewander.github.io/explore-flask-zh/7-blueprints.html 蓝图 什么是蓝图? 一个蓝图定义了可用于单个应用的视图,模板,静态 ...
- Oracle 性能调优之:使用 V$SQL_PLAN 视图查询内存中的执行计划
V$SQL_PLAN视图提供了一种方法,可用于检查仍位于库高速缓存的游标的执行计划.此视图中的信息与 PLAN_TABLE 视图中的信息非常类似.但是,EXPLAIN PLAN 显示的是执行相应语句时 ...
- [Python] String strip() Method
Description The method strip() returns a copy of the string in which all chars have been stripped fr ...
- 102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree
▶ 有关将一棵二叉树转化为二位表的题目,一模一样的套路出了四道题 ▶ 第 102 题,简单的转化,[ 3, 9, 20, null, null, 15, 7 ] 转为 [ [ 15, 7 ] , [ ...
- myeclipse通过数据表生成jpa或hibernate实体
1. 创建数据库连接 2. 选择表 3. 生成 hibernate mapping 4. 生产jpa
- 通过django创建数据库的方法
在models 文件中实现 a. from django.db import models class UserInfo(models.Model): #id列, 自增, 主键 #用户名列, 字符串类 ...
- c++经典排序算法全集(转)
C++排序算法全集 排序算法是一种基本并且常用的算法.由于实际工作中处理的数量巨大,所以排序算法对算法本身的速度要求很高. 一.简单排序算法 由于程序比较简单,所以没有加什么注释.所有的程序都给出了完 ...