Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy
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) {
if (root == nullptr)
return true;
return isSymmetricCore(root->left, root->right);
}
bool isSymmetricCore(TreeNode* leftRoot, TreeNode* rightRoot) {
if (leftRoot == nullptr && rightRoot == nullptr)
return true;
else if (leftRoot == nullptr || rightRoot == nullptr)
return false;
if (leftRoot->val == rightRoot->val)
return isSymmetricCore(leftRoot->left, rightRoot->right) && isSymmetricCore(leftRoot->right, rightRoot->left);
return false;
}
};
Leetcode之101. Symmetric Tree Easy的更多相关文章
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 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 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【一天一道LeetCode】#101. Symmetric Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】101 - Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode OJ 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 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
随机推荐
- flask学习导航主页
我就学习了网易课堂的知了Flaskk. 十分感谢. └—01-Flask视图和URL ├—课时001.[Flask预热]课程介绍 ├—课时002.[Flask预热]Flask课程准备工作 ├—课时00 ...
- DataGrip 配置连接mysql8.0的注意事项
在mysql8.0版本之后,依赖驱动文件不同,使用之前的连接配置,会无法正常的连接数据库. 已连接本地Mysql数据库为例,需在 URL配置项 jdbc:mysql://localhost:3306? ...
- 【Android-网络通讯】 客户端与.Net服务端Http通讯
以登陆系统为例: 一.创建服务端程序 1.打开VS2012,新建项目,创建ASP.NET WEB应用程序 ,命名为MyApp 2.添加新建项,选择一般处理程序,创建Login.ashx C# Code ...
- parseInt parseFloat isNaN Number 区别和具体的转换规则及用法
原文链接:https://blog.csdn.net/wulove52/article/details/84953998 在javascript 我经常用到,parseInt.parseFloat.N ...
- 了解Springboot加载文件机制
https://blog.csdn.net/u014044812/article/details/84256764(
- 【线性代数】2-1:解方程组(Ax=b)
title: [线性代数]2-1:解方程组(Ax=b) toc: true categories: Mathematic Linear Algebra date: 2017-08-31 15:08:3 ...
- ie11 div不显示背景颜色解决方案
我的一个场景就是,一个空的div,但是想加个背景颜色,方案就是在div加个空content,利用before属性加上背景<div class="hilan"></ ...
- [bat]删除文件
删除文件 del /f /s /q D:\HRG\NEW_Vn\CSV\*.meta 删除空文件夹 只能先删完文件夹中的文件,再删除空文件夹 rd /s /q D:\HRG\NEW_Vn\CSV\ 脚 ...
- 平衡Dom总结
介绍: 新的项目中有些Dom元素需要和画布保持统一个适配比例 项目地址: 宝岛之光-台湾偶像剧 遇到的问题 H5项目使用Canvas, 适配采用保持宽高比例, 上下或者左右留白方式 在项目中有些Dom ...
- Linux-ubuntu英文版输入法不能切换中文输入法问题解决办法
1:System Settings中点击Language Support 2. 3. 4. 5. 6. 7.注意不要勾选Only Show Current Language