【一天一道LeetCode】#101. Symmetric Tree
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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
Note:
Bonus points if you could solve it both recursively and iteratively.
(二)解题
本题大意:判断一颗树是不是左右对称
解题思路一:分别求二叉树的前序和后序遍历,他们是互为反转数组。
/**
* 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) {
vector<int> pre;
vector<int> post;
preOrderTree(root , pre);
postOrderTree(root , post);
for(int i = 0 ,j=post.size()-1; i<pre.size(),j>=0;i++,j--)//一个正向,一个反向
{
if(pre[i] != post[j]) return false;//不相等就返回false
}
return true;
}
void preOrderTree(TreeNode* root , vector<int> &pre)//前序遍历
{
if(root==NULL) {pre.push_back(0);return;}
pre.push_back(root->val);
preOrderTree(root->left,pre);
preOrderTree(root->right,pre);
}
void postOrderTree(TreeNode* root , vector<int> &post)//后续遍历
{
if(root==NULL) {post.push_back(0);return;}
postOrderTree(root->left,post);
postOrderTree(root->right,post);
post.push_back(root->val);
}
};
解题思路二:将前序遍历和后序遍历合起来一起考虑,对于一个节点,如果他们相等,就判断左右子树是否为镜像!
/**
* 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) {
return isMirror(root,root);
}
bool isMirror(TreeNode* root1,TreeNode* root2)
{
if(root1==NULL) return root2==NULL;
if(root2==NULL) return false;
if(root1->val!=root2->val) return false;
return isMirror(root1->left,root2->right) && isMirror(root1->right,root2->left);//左子树和右子树是否成镜像
}
};
【一天一道LeetCode】#101. Symmetric Tree的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Java [Leetcode 101]Symmetric Tree
题目描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode 101. Symmetric Tree(easy)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- MySQL数据类型DECIMAL用法
MySQL DECIMAL数据类型用于在数据库中存储精确的数值.我们经常将DECIMAL数据类型用于保留准确精确度的列,例如会计系统中的货币数据. 要定义数据类型为DECIMAL的列,请使用以下语法: ...
- Lucene初体验——Hello Word实现
1.创建索引 /** * 建立索引 */ public void index(){ IndexWriter writer=null; try { //1.创建Directory //Directory ...
- 地址下拉框,需要js级联js
function area() { _url = "/ashx/DropDownControl.ashx"; _swType = "GetArea"; _z = ...
- .NET Core 网络数据采集 -- 使用AngleSharp做html解析
有这么一本Python的书: <<Python 网络数据采集>> 我准备用.NET Core及第三方库实现里面所有的例子. 这是第一部分, 主要使用的是AngleSharp: ...
- java基础复习+大数运算
String: Array: 下面分别是大数加法,加法,乘法,取模
- PHP 安全 E-mail
PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html> <body> <?php if (isset($_REQUEST['email'])) / ...
- MongoDB 查询分析
MongoDB 查询分析可以确保我们建议的索引是否有效,是查询语句性能分析的重要工具. MongoDB 查询分析常用函数有:explain() 和 hint(). 使用 explain() expla ...
- [Gradle系列]Gradle发布module库到jCenter, 并构建自己的企业Maven私服
Tamic 作者: http://blog.csdn.net/sk719887916/article/details/53224544 前言 andorid开发者经常会看到xx公司发布了xx项目,xx ...
- Redis之(一)初识Redis
1.Redis概述 我们知道,内存是电脑主板上的存储部件,用于存储当前正在使用的数据和程序,CPU可以与内存直接沟通,所以访问速速非常高:而外存数据必须加载到内存以后程序才能使用.如果把CPU当做一个 ...
- Mac入门
Mac入门 桌面 windows桌面有图标罗列 Mac桌面有Dock 菜单栏 感觉上和Windows系统的底部菜单栏有点像,但是却略有不同,Mac的菜单栏默认在顶部 左侧的一些功能是固定不变的,跟随当 ...