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

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. Bootstrap-Plugin:插件概览

    ylbtech-Bootstrap-Plugin:插件概览 1.返回顶部 1. Bootstrap 插件概览 在前面 布局组件 章节中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 j ...

  2. Java利用ScriptEngineManager对计算公式的支持

    1.ScriptEngineManager是JDK6提出的相关方法,这方式的主要目的就是用来对脚本语言的处理.这里只是简单介绍一下对我们常用的数学公式的应用. 2.ScriptEngineManage ...

  3. javascript中 关于eval的那些事

    javascript中的eval是一个非常灵活,但是灵活是伴随着风险的. 一.下面我们来看看那使用eval声明变量的问题. function test(x){ eval("var a=x;& ...

  4. 自动执行单元测试maven插件

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-suref ...

  5. OpenCL Hello World

    ▶ OpenCL 的环境配置与第一个程序 ● CUDA 中自带 OpenCL 需要的头文件和库,直接拉近项目里边去就行:AMD 需要下载 AMD APP SDK(https://community.a ...

  6. 最值得学习阅读的10个C语言开源项目代码

    阅读优秀代码是提高开发人员修为的一种捷径-- 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网 ...

  7. C++实现:把一个文件夹里的冗余文件(.txt)删除

    代码很简单,调用了MFC里的几个函数.这里的冗余判断,是要遍历文件内容,进行两两比较. 需要注意的地方有两点: 1.源文件里头文件<afx.h>必须放在最前面.这里是为了避免nafxcwd ...

  8. Android工程目录结构

    ----------siwuxie095 首先创建一个简单的项目:MainActivity 工程目录结构一览: 工程目录结构介绍: 1.manifests目录 里面有一个AndroidManifest ...

  9. 二叉树中和为某一值的路径 (java)

    问题描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 题解 import java.util.Arra ...

  10. loadrunner--web_url函数用法

    web_url语法:        Int Web_url(const char *name, const char * url, <Lists of Attributes>, [EXTR ...