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 ...
随机推荐
- dcm4che-core导包失败! mvn pom文件导包总是失败
原因可能是所导的包不在共有项目中,可能在个人项目中,需要添加远程仓库 <!--远程仓库部署--><repositories> <repository> <id ...
- STM32调试利器之ITM
原创: Osprey 鱼鹰谈单片机 2月17日 STM32 有一个代码跟踪功能,即 ITM,这个调试功能非常强大,可以替代串口输入输出功能,而且只需要占用一根 I/O 线就可以实现.当然它的好处不仅 ...
- 计数dp做题笔记
YCJS 3924 饼干 Description 给定一个长度为\(n\)的序列,序列每个元素的取值为\([1,x]\),现在给定\(q\)个区间,求在所有取值方案中,区间最小值的最大值的期望是多少? ...
- oracle汉字排序
oracle在9i之前是对汉字的排序是按照二进制编码进行排序的,很不适合我们的国情,在oracle9i之后,汉字的排序方式有了以下三种方式: 1.使用拼音排序 NLS_SORT=SC ...
- vscode 远程编辑文件
操作 安装扩展 remote-vscode 配置ssh 转发: 添加 RemoteForward 52698 127.0.0.1:52698 到 ~/.ssh/config: # Read more ...
- kubernetes Configmap secret的使用
kubernetes configmap 核心作用是让配置信息和镜像解耦,pod可以使用configmap的数据生成配置文件.如果后端的pod配置文件要改变时,只需要更改下configmap里面的数据 ...
- FtpHelper.cs
网上找了好多,感觉还是自己这个使用起来方便点,记录一下! using System; using System.Collections; using System.IO; using System.L ...
- 2019.6.20 校内测试 NOIP模拟 Day 1 分析+题解
这次是zay神仙给我们出的NOIP模拟题,不得不说好难啊QwQ,又倒数了~ T1 大美江湖 这个题是一个简单的模拟题. ----zay 唯一的坑点就是打怪的时候计算向上取整时,如果用ceil函数一 ...
- java中判断空字符串和null的判断方法
简单总结几个方法: 1.直观的: if(s == null ||"".equals(s)); //先判断是否对象,再判断是否是空字符串 2.比较字符串长度, 效率高, 比较绕: i ...
- Leetcode题目139.单词拆分(动态规划-中等)
题目描述: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词.你可以假设字典 ...