[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 ...
随机推荐
- 文件计数--file_count.sh
countFiles(){ path=$1 pathName=`ls $path` sum=0 for i in $pathName do num=`ls $path$i |wc -l` sum=$[ ...
- Charles使用1
Charles是一款比较常用的全平台的网络封包街区工具,而我们在做移动开发的时候,我们为了调试.测试.分析等目的,经常需要和服务端的网络通讯协议打交道.Charles可以帮我们截取网络数据包来进行分析 ...
- python渗透测试工具包
网络 Scapy, Scapy3k: 发送,嗅探,分析和伪造网络数据包.可用作交互式包处理程序或单独作为一个库.pypcap, Pcapy, pylibpcap: 几个不同 libpcap 捆绑的py ...
- C#调用Oracle带输出数据集的存储过程
1.创建一个带输出数据集的Oracle存储过程 create or replace procedure PRO_test(in_top in number,cur_out out sys_refcur ...
- cesium初始化参数
var viewer = new Cesium.Viewer('cesiumContainer',{ animation:false, //动画控制不显示 //baseLayerPicker:fals ...
- 20165233 Java第二、三章学习总结
2017-2018-2 <Java程序设计>第二周学习总结 教材学习内容总结 第二.三章 ch2 标识符与关键字 基本数据类型: 逻辑类型:boolean 整数类型:int.byte.sh ...
- 26.OGNL与ValueStack(VS)-N语法top语法
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html N语法[0]:<s:property value="[0]& ...
- MySQL GTID (四)
七. GTID的限制以及解决方案 7.1 事务中混合多个存储引擎,会产生多个GTID. 当使用GTID,在同一个事务中,更新包括了非事务引擎(MyISAM)和事务引擎(InnoDB)表的操作,就会导致 ...
- Eclipse去掉对jQuery的错误提示
1.I have found that I can leave the JavaScript Validator enable and ignore specific files by adding ...
- 在浏览器输入URL后发生了什么?
摘录部分一:https://www.cnblogs.com/kongxy/p/4615226.html 从输入URL到浏览器显示页面发生了什么 当在浏览器地址栏输入网址,如:www.baidu.com ...