这次我觉得我的智商太低,想了很久才写出来。题目是让求镜像二叉树判断,题目如下:

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.

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
\
5

The 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的更多相关文章

  1. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

  2. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  3. 【leetcode】Symmetric Tree

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  4. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  5. LeetCode 101. Symmetric Tree (对称树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  6. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  7. [LeetCode 题解]: Symmetric Tree

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述   Given a ...

  8. leetcode 101 Symmetric Tree ----- java

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  9. LeetCode 101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

随机推荐

  1. 黄聪:WIN7下回收站不小心删除的文件怎么恢复,免费数据恢复软件下载

    上网找了半天,大多数是收费的,总算找到一款免费的,已经帮我恢复了数据了,在此分享给大家. 注意:只能恢复7天内的,而且数据误删了,就尽量不要再修改你那个盘符的数据了,免得覆盖了! 我用的数据恢复软件R ...

  2. [转]使用Android-Studio 开发Android 程序

    界面主题可以从这里下载:http://color-themes.com/ 杂七杂八(包含Android-Studio 自身):http://www.androiddevtools.cn/ genymo ...

  3. postman-3http请求

    http消息是服务器和客户端之间交换数据的方式. 有2种类型的消息: 请求:由客户端发送用来触发服务器的动作. 响应:来自服务器的应答. https://developer.mozilla.org/z ...

  4. 将各种格式的数据转换成XML

    public class DataToXml    {               /// <summary>        /// 将DataTable对象转换成XML字符串       ...

  5. CEP实时分析模型

    大数据量的实时分析场景: 股票实时分析系统:大数据量(基于内存),要求分析汇总数据

  6. python 构造mysql爆破器

    前言: 今天已经期末考完,睡了个觉起床写了个 mysql爆破器. 思路: 1.爆破用户->用户存在的话不会报错反之报错 2.爆破密码->密码正确不会报错反之报错 3.用户名和密码一起爆破- ...

  7. selenium+python自动化79-文件下载(SendKeys)

    前言 文件下载时候会弹出一个下载选项框,这个弹框是定位不到的,有些元素注定定位不到也没关系,就当没有鼠标,我们可以通过键盘的快捷键完成操作. SendKeys库是专业的处理键盘事件的,所以这里需要用S ...

  8. Javascript 日期 加减

    //create the date var myDate = new Date(); //add a day to the date myDate.setDate(myDate.getDate() + ...

  9. Visio2013 64位下载安装以及破解激活教程

    特别说明:以下教程如果未能破解激活,请在断网条件下安装破解!!!! 安装: Visio2013 professional版下载地址:https://pan.baidu.com/s/1gzwcGTevV ...

  10. SystemColors 成员

    名称 说明 ActiveBorder 获取 Color 结构,它是活动窗口边框的颜色. ActiveCaption 获取 Color 结构,它是活动窗口标题栏的背景色. ActiveCaptionTe ...