[抄题]:

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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following [1,2,2,null,3,null,3] is not:

    1
/ \
2 2
\ \
3 3

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

不知道有什么数学规律的时候,分情况讨论:分左右两边

[一句话思路]:

  1. 以为要写很多right,left:只要写基本的两个点能一直嵌套递归了,这就是recursion的作用啊!
  2. 而且必须两点之间有关系才能递归,一个点只能往下继承

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. recursion是嵌套调用,是用函数来实现的,不是用等号。要写函数名,看来还没理解

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

true false写之前想清楚

[总结]:

recursion是嵌套调用,是用函数来实现的,不是用等号。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

public boolean isSymmetricHelper(TreeNode left, TreeNode right) {
//all null
if (left == null && right == null) {
return true;
}
//one null
if (left == null || right == null) {
return false;
}
//not same
if (left.val != right.val) {
return false;
}
//same
return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
//don't forget the function name
}

Helper(TreeNode left, TreeNode right)有左右俩参数

[其他解法]:

stack能写死。

非递归就只要学pre-order in-order就行了

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
//corner case
if (root == null) {
return true;
}
return isSymmetricHelper(root.left, root.right);
} public boolean isSymmetricHelper(TreeNode left, TreeNode right) {
//all null
if (left == null && right == null) {
return true;
}
//one null
if (left == null || right == null) {
return false;
}
//not same
if (left.val != right.val) {
return false;
}
//same
return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
//don't forget the function name
}
}

对称二叉树 · symmetric binary tree的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  3. 数据结构-二叉树(Binary Tree)

    1.二叉树(Binary Tree) 是n(n>=0)个结点的有限集合,该集合或者为空集(空二叉树),或者由一个根节点和两棵互不相交的,分别称为根节点的左子树和右子树的二叉树组成.  2.特数二 ...

  4. [Swift]LeetCode101. 对称二叉树 | Symmetric Tree

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

  5. [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. [Swift]LeetCode226. 翻转二叉树 | Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...

  8. [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree

    Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ...

  9. [Swift]LeetCode655. 输出二叉树 | Print Binary Tree

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

随机推荐

  1. 【转】Eclipse的启动问题【an error has occurred see the log file】

    原文网址:http://coderlin.blog.51cto.com/7386328/1275215 方法1: 今天打开Eclipse的时候出现来了一个问题,导致了Eclipse打不开 错误的提示是 ...

  2. Web 漏洞分析与防御之 CSRF(二)

    原文地址:Web 漏洞分析与防御之 CSRF(二) 博客地址:http://www.extlight.com 一.全称 跨站请求伪造(Cross-site Request Forgery) 二.原理 ...

  3. 【译】GNU Radio How to write a block 【如何开发用户模块及编写功能块】

    本文讲解如何在GNU Radio中添加用户开发的信号处理模块,译文如有不当之处可参考原文地址:http://gnuradio.microembedded.com/outoftreemodules Ou ...

  4. Unit06: Spring对JDBC的 整合支持 、 Spring+JDBC Template、Spring异常处理

    Unit06: Spring对JDBC的 整合支持 . Spring+JDBC Template .Spring异常处理 1. springmvc提供的异常处理机制 我们可以将异常抛给spring框架 ...

  5. shell常用测试命令

    预定义变量: 预定义变量是由Bash程序预先定义好的一类特殊变量,用户只能使用预定义变量,而不能创建新的预定义变量,也不能直接为预定义变量赋值.预定义比变量使用"$"符合和另一个符 ...

  6. [转载]Linux下getopt()函数的简单使用

    转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...

  7. SQL Server数据库常用的T-SQL命令

    1. 查看数据库的版本 select @@version 2.查看数据库所在机器操作系统参数 exec master..xp_msver 3. 查看数据库启动的参数 sp_configure 4.查看 ...

  8. flv格式详解+实例剖析

    简介 FLV(Flash Video)是现在非常流行的流媒体格式,由于其视频文件体积轻巧.封装播放简单等特点,使其很适合在网络上进行应用,目前主流的视频网站无一例外地使用了FLV格式.另外由于当前浏览 ...

  9. python在文件中输入整数

    a=[] b=[] for i in range(len(predicted)): b.append((int)(float(predicted[i]))) a.append(int(test_set ...

  10. 杂项: Redis

    ylbtech-杂项: Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 1. 定义返回顶部 re ...