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

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. MikroTik-ROS-无线设备传输距离

    近期在MikroTik官网论坛给出了官方无线成品设备的最大传输距离. 以下设备列表基于理想的环境条件,包括干扰,天气,校准精度等因素,表给出了基于802.11ac或802.11n在获取最大传输带宽的情 ...

  2. jvectormap地图开发和制作任意国家地图

    jvectormap官网上提供了世界地图和很多国家的地图,但不是所有国家的地图都有,比如沙特阿拉伯的国家地图就没有,怎么办呢? 在http://www.amcharts.com/svg-maps/上下 ...

  3. css踩过的坑

    1. 英文内容会撑破容器而中文内容是可以自动换行的 原因: 因为数学规则因素,中文韩文朝鲜文等可以在任何字之后断行的,而英文却不能把一个单词拆开换行,只能撑破容器 解决: 用word-break: b ...

  4. Python算法应用实战之队列详解

    队列是一种先进先出(First-In-First-Out,FIFO)的数据结构.队列被用在很多地方,比如提交操作系统执行的一系列进程.打印任务池等,一些仿真系统用队列来模拟银行或杂货店里排队的顾客.下 ...

  5. 机器视觉和Tesseract

    机器视觉 从 Google 的无人驾驶汽车到可以识别假钞的自动售卖机,机器视觉一直都是一个应用广 泛且具有深远的影响和雄伟的愿景的领域. 我们将重点介绍机器视觉的一个分支:文字识别,介绍如何用一些 P ...

  6. 串口通信,帧与帧之间的时间间隔问题?9600波特率,帧将各在20ms以上

  7. LInux Centos7 重装yum

    Linux yum默认安装python2.7,犹豫2020年之后不更新, 安装python3.6,出现了yum运行失败, 因为yum是依赖python的 1:先删除原有YUM rpm -aq|grep ...

  8. 8.3.2018 1 Quick and dirty 快而脏的快餐

    Quick and dirty  快而脏的快餐 BEIJING  北京 Food delivery is a booming business. Waste is piling up, too  送餐 ...

  9. Redis AOF文件

    [Redis AOF文件] 1.关于AOF AOF 持久化记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原数据集. AOF 文件中的命令全部以 Redis 协议的格式来保存 ...

  10. go_组合接口

    main函数入口 package main import ( "fmt" "learngo/retriever/mock" "learngo/retr ...