对称二叉树 · symmetric binary tree
[抄题]:
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
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
不知道有什么数学规律的时候,分情况讨论:分左右两边
[一句话思路]:
- 以为要写很多right,left:只要写基本的两个点能一直嵌套递归了,这就是recursion的作用啊!
- 而且必须两点之间有关系才能递归,一个点只能往下继承
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
		二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ... 
- 遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
		遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ... 
- 数据结构-二叉树(Binary Tree)
		1.二叉树(Binary Tree) 是n(n>=0)个结点的有限集合,该集合或者为空集(空二叉树),或者由一个根节点和两棵互不相交的,分别称为根节点的左子树和右子树的二叉树组成. 2.特数二 ... 
- [Swift]LeetCode101. 对称二叉树 | Symmetric Tree
		Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ... 
- [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 ... 
- [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 ... 
- [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 ... 
- [Swift]LeetCode654. 最大二叉树 | Maximum Binary Tree
		Given an integer array with no duplicates. A maximum tree building on this array is defined as follo ... 
- [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 ... 
随机推荐
- poj 1637 Sightseeing tour——最大流+欧拉回路
			题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ... 
- bzoj1089严格n元树
			题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1089 这是一种套路:记录“深度为 i ”的话,转移需要讨论许多情况:所以可以记录成“深度&l ... 
- DataOutput接口实现类有:
			FSDataOutputStream final FSDataOutputStream create = fs.create(path); 
- jdk1.8新特性之方法引用
			方法引用其实就是方法调用,符号是两个冒号::来表示,左边是对象或类,右边是方法.它其实就是lambda表达式的进一步简化.如果不使用lambda表达式,那么也就没必要用方法引用了.啥是lambda,参 ... 
- (OPC Client .NET 开发类库)网上很多网友都有提过,.NET开发OPC Client不外乎下面三种方法
			1. 背景 OPC Data Access 规范是基于COM/DCOM定义的,因此大多数的OPC DA Server和client都是基于C++开发的,因为C++对COM/DCOM有最好的支持.现在, ... 
- sprinboot+redis
			(1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> < ... 
- Servlet 前端后台交互
			一. URL地址传值 1.1. 地址传值 http://localhost:8080/xj/123/name.json servlet 对应接受方法 @RequestMapping(value=& ... 
- 同步机制之--java CyclicBarrier 循环栅栏
			CyclicBarrier介绍一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待 ... 
- WebStorm ES6 语法支持设置
			ECMAScript 6是JavaScript语言的下一代标准,已经在2015年6月正式发布了.Mozilla公司将在这个标准的基础上,推出JavaScript 2.0.ES6的目标,是使得JavaS ... 
- nginx与tomcat整合
			nginx与tomcat整合 1. 在/usr/local/nginx/conf下面添加文件proxy.conf # cat /usr/local/nginx/confg/proxy.conf p ... 
