【Leetcode】对称二叉树
递归法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL)
return true; if( isMirror(root->left, root->right))
return true;
else
return false;
} bool isMirror(TreeNode* lc, TreeNode* rc) {
if(lc == NULL && rc == NULL)
return true; if(lc == NULL || rc == NULL)
return false;
else {
if (lc->val == rc->val)
return (isMirror(lc->left, rc->right) && isMirror(lc->right, rc->left));
else
return false;
}
}
};
时间复杂度:
O(n),因为算法过程要遍历树的每一个节点,节点数量为n。
空间复杂度:
O(n),递归函数使用的栈空间与树的层数有关。如果树为线性结构,其层数为n,所以空间复杂度为O(n)
迭代法
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL)
return true;
queue< TreeNode* > q1;
queue< TreeNode* > q2;
q1.push(root->left);
q2.push(root->right);
while(q1.size()> && q2.size()>) {
TreeNode* n1 = q1.front();
TreeNode* n2 = q2.front();
q1.pop();
q2.pop();
if(n1 == NULL && n2 == NULL)
continue;
if(n1 == NULL || n2 == NULL)
return false;
if(n1->val == n2->val) {
q1.push(n1->left);
q2.push(n2->right);
q1.push(n1->right);
q2.push(n2->left);
}
else
return false;
}
return true;
}
};
时间复杂度:
O(n) 要遍历每一个节点;
空间复杂度:
搜索队列需要额外的空间。在最糟糕情况下,我们不得不向队列中插入 O(n)个结点(???)。因此,空间复杂度为 O(n)。
【Leetcode】对称二叉树的更多相关文章
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- LeetCode 101 对称二叉树的几种思路(Python实现)
对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \3 4 4 3 但是下面这个 [1,2,2 ...
- Java实现 LeetCode 101 对称二叉树
101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...
- Leecode刷题之旅-C语言/python-101对称二叉树
/* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetri ...
- C语言递归之对称二叉树
题目描述 给定一个二叉树,检查它是否是镜像对称的. 示例 二叉树 [1,2,2,3,4,4,3] 是对称的. / \ / \ / \ [1,2,2,null,3,null,3] 则不是镜像对称的. / ...
- 【leetcode-101】 对称二叉树
101. 对称二叉树 (1过) 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [ ...
- 【洛谷P5018】对称二叉树
题目大意:定义对称二叉树为每个节点的左右子树交换后与原二叉树仍同构的二叉树,求给定的二叉树的最大对称二叉子树的大小. 代码如下 #include <bits/stdc++.h> using ...
- 判断对称二叉树 python代码
对称二叉树的含义非常容易理解,左右子树关于根节点对称,具体来讲,对于一颗对称二叉树的每一颗子树,以穿过根节点的直线为对称轴,左边子树的左节点=右边子树的右节点,左边子树的右节点=左边子树的左节点.所以 ...
- LeetCode 107 ——二叉树的层次遍历 II
1. 题目 2. 解答 与 LeetCode 102 --二叉树的层次遍历 类似,我们只需要将每一层的数据倒序输出即可. 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level ...
- LeetCode:二叉树的后序遍历【145】
LeetCode:二叉树的后序遍历[145] 题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
随机推荐
- [转帖]什么是 LLVM?Swift, Rust, Clang 等语言背后的支持
要了解用于以编程方式生成机器原生代码的编译器框架是如何让新语言的推出以及对现有的语言进行增强比以往更加容易了. https://www.oschina.net/translate/what-is-ll ...
- 解决 Illegal DefaultValue null for parameter type integer 异常
该异常是由 swagger 引起的 swagger 版本 1.9.2 解决原因:重新导入 swagger-annotations 和 swagger-models 版本 为 1.5.21 pom.xm ...
- 基于Spark的电影推荐系统
数据文件: u.data(userid itemid rating timestamp) u.item(主要使用 movieid movietitle) 数据操作 把u.data导入RDD, t ...
- 从入门到自闭之Python三大器--迭代器
函数名的第一类对象(概述): 使用方式: 函数名可以当做值赋值给变量 def func(): print(1) print (func) #查看函数的内存地址 a = func print (a) # ...
- Two progressions CodeForces - 125D (暴力)
大意: 给定序列, 求划分为两个非空等差序列. 暴搜, 加个记忆化剪枝. #include <iostream> #include <sstream> #include < ...
- vue项目报错1 Vue is a constructor and should be called with the `new` keyword && jquery.js?eedf:3850 Uncaught TypeError: this._init is not a function...
Vue is a constructor and should be called with the `new` keyword Uncaught TypeError: this._init is n ...
- JS实现hasClass addClass removeClass
// 判断class有无 function hasClass(ele, cls) { if (ele) { cls = cls || '' if (cls.replace(/\s/g, '').len ...
- 62. Unique Paths (JAVA)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- iptables-save - 保存 IP Tables
总览 SYNOPSIS iptables-save [-c] [-t table] 描述 DESCRIPTION iptables-save 用来将 IP Table 转储为可以简单解析的格式,输出到 ...
- JavaNIO
Java New IO 简称 nio,在jdk1.4提供了新的api,有如下特性: 1.为所有原始类型提供缓存支持 2.字符集编解码解决方案 3.Channel:新的原始io抽象 4.支持锁和内存映射 ...