Commit fb2465e2 authored by 程序员吴师兄's avatar 程序员吴师兄
Browse files

整理部分文件

parent 1a2d7393
# LeetCode 第 103 号问题:二叉树的锯齿形层次遍历
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 103 号问题:二叉树的锯齿形层次遍历。题目难度为 Medium,目前通过率为 43.8% 。
### 题目描述
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 `[3,9,20,null,null,15,7]`,
```
3
/ \
9 20
/ \
15 7
```
返回锯齿形层次遍历如下:
```
[
[3],
[20,9],
[15,7]
]
```
### 题目解析
该问题需要用到**队列**,与之前的[二叉树的层次遍历](https://xiaozhuanlan.com/topic/8579460312)类似,不同点在于在偶数层需要翻转一下。
- 建立一个queue
- 先把根节点放进去,这时候找根节点的左右两个子节点
- 去掉根节点,此时queue里的元素就是下一层的所有节点
- 循环遍历,将结果存到一个一维向量里
- 遍历完之后再把这个一维向量存到二维向量里
- 如果该层为偶数层,则reverse翻转一下
- 以此类推,可以完成层序遍历
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/xuoqo.gif)
### 代码实现
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/7mnmj.png)
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 107 号问题:二叉树的层次遍历 II
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 107 号问题:二叉树的层次遍历 II。题目难度为 Easy,目前通过率为 55.8% 。
### 题目描述
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
例如:
给定二叉树 `[3,9,20,null,null,15,7]`,
```
3
/ \
9 20
/ \
15 7
```
返回其自底向上的层次遍历为:
```
[
[15,7],
[9,20],
[3]
]
```
### 题目解析
该问题需要用到**队列**,解法与上篇[每天一算:Binary Tree Level Order Traversal](https://xiaozhuanlan.com/topic/8579460312)类似,区别在于最后存储方式的不同。
- 建立一个 queue
- 先把根节点放进去,这时候找根节点的左右两个子节点
- 去掉根节点,此时queue里的元素就是下一层的所有节点
- 用 for 循环遍历,将结果存到一个一维向量里
- 遍历完之后再把这个一维向量**插入**到二维向量里
- 以此类推,可以完成层序遍历
### 动画描述
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/varp8.gif)
### 代码实现
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/9iccc.png)
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 110 号问题:平衡二叉树
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 上第 110 号问题:平衡二叉树。
### 题目描述
给定一个二叉树,判断它是否是高度平衡的二叉树。
### 题目解析
采取**后序遍历**的方式遍历二叉树的每一个结点。
在遍历到一个结点之前已经遍历了它的左右子树,那么只要在遍历每个结点的时候记录它的深度(某一结点的深度等于它到叶结点的路径的长度),就可以一边遍历一边判断每个结点是不是平衡的。
### 动画描述
待补充
### 代码实现
```java
class Solution {
private boolean isBalanced = true;
public boolean isBalanced(TreeNode root) {
getDepth(root);
return isBalanced;
}
public int getDepth(TreeNode root) {
if (root == null)
return 0;
int left = getDepth(root.left);
int right = getDepth(root.right);
if (Math.abs(left - right) > 1) {
isBalanced = false;
}
return right > left ? right + 1 : left + 1;
}
}
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
# LeetCode 第 125 号问题:验证回文串
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com
题目来源于 LeetCode 第 125 号问题:验证回文串。这道题目是 **初级程序员** 在面试的时候经常遇到的一道算法题,而且面试官喜欢面试者手写!
### 题目描述
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
**说明:**本题中,我们将空字符串定义为有效的回文串。
**示例 1:**
```
输入: "A man, a plan, a canal: Panama"
输出: true
```
**示例 2:**
```
输入: "race a car"
输出: false
```
### 题目解析
先理解一个概念:所谓回文,就是一个正读和反读都一样的字符串。
先假设是验证单词 `level` 是否是回文字符串,通过概念涉及到 正 与 反 ,那么很容易想到使用双指针,从字符的开头和结尾处开始遍历整个字符串,相同则继续向前寻找,不同则直接返回 false。
而这里与单独验证一个单词是否是回文字符串有所区别的是加入了 空格 与 非字母数字的字符,但实际上的做法一样的:
一开始先建立两个指针,left 和 right , 让它们分别从字符的开头和结尾处开始遍历整个字符串。
如果遇到非字母数字的字符就跳过,继续往下找,直到找到下一个字母数字或者结束遍历,如果遇到大写字母,就将其转为小写。
当左右指针都找到字母数字时,可以进行比较的时候,比较这两个字符,如果相等,则两个指针向它们的前进方向挪动,然后继续比较下面两个分别找到的字母数字,若不相等,直接返回 false。
### 动画描述
![](../Animation/animation.gif)
### 代码实现
注:`isLetterOrDigit ` 方法确定指定的字符是否为字母或数字。
```java
class Solution {
public boolean isPalindrome(String s) {
if(s.length() == 0)
return true;
int l = 0, r = s.length() - 1;
while(l < r){
//确定指定的字符是否为字母或数字
if(!Character.isLetterOrDigit(s.charAt(l))){
l++;
}else if(!Character.isLetterOrDigit(s.charAt(r))){
r--;
}else{
if(Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r)))
return false;
l++;
r--;
}
}
return true;
}
}
```
![](../../Pictures/qrcode.jpg)
\ No newline at end of file
...@@ -8,9 +8,13 @@ ...@@ -8,9 +8,13 @@
我会尽力将 LeetCode 上所有的题目都用动画的形式演示出来,计划用 3 到 4 年时间去完成它,期待与你见证这一天! 我会尽力将 LeetCode 上所有的题目都用动画的形式演示出来,计划用 3 到 4 年时间去完成它,期待与你见证这一天!
文章最新首发于微信公众号 **五分钟学算法** ,您可以关注获取最新的文章。 文章最新首发于微信公众号 **图解面试算法**,您可以关注获取最新的文章。
我已经将所有文章同步到了我的个人博客,如果国内访问 GitHub 较慢(图片裂开),可以访问这个地址:[https://www.cxyxiaowu.com/likou/leetcode](https://www.cxyxiaowu.com/likou/leetcode) ![](Pictures/qrcode.jpg)
文章同步博客地址:https://www.algomooc.com
## 汇总 ## 汇总
...@@ -75,7 +79,7 @@ ...@@ -75,7 +79,7 @@
| 219 | [存在重复元素 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第219号问题:存在重复元素II.md) | | | 219 | [存在重复元素 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第219号问题:存在重复元素II.md) | |
| 229 | [求众数II](https://mp.weixin.qq.com/s/ObO4eQbjp1s1g_WXPkjixQ) | | | 229 | [求众数II](https://mp.weixin.qq.com/s/ObO4eQbjp1s1g_WXPkjixQ) | |
| 231 | [2的幂](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第231号问题:2的幂.md) | | | 231 | [2的幂](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第231号问题:2的幂.md) | |
| 232 | [使用栈实现队列](https://mp.weixin.qq.com/s/j6w94_PjvsL9Dip_xBcqcg) | | | 232 | [使用栈实现队列](https://mp.weixin.qq.com/s/j6w94_PjvsL9Dip_xBcqcg) | |
| 237 | [删除链表中的节点](https://mp.weixin.qq.com/s/2XdUeDNblryFpXpTUgsaMQ) | | | 237 | [删除链表中的节点](https://mp.weixin.qq.com/s/2XdUeDNblryFpXpTUgsaMQ) | |
| 239 | [滑动窗口最大值](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第239号问题:滑动窗口最大值.md) | | | 239 | [滑动窗口最大值](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第239号问题:滑动窗口最大值.md) | |
| 242 | [有效的字母异位词](https://mp.weixin.qq.com/s/o5HTxmOgpftSaQdebS9zyQ) | | | 242 | [有效的字母异位词](https://mp.weixin.qq.com/s/o5HTxmOgpftSaQdebS9zyQ) | |
...@@ -102,27 +106,10 @@ ...@@ -102,27 +106,10 @@
| 1025 | [除数博弈](https://mp.weixin.qq.com/s/0u6z02QYj1OpAwf54k8-Dw) | | | 1025 | [除数博弈](https://mp.weixin.qq.com/s/0u6z02QYj1OpAwf54k8-Dw) | |
| 1099 | [小于 K 的两数之和](https://mp.weixin.qq.com/s/S6BbLeP_th_9JheNX7NN-w) | | | 1099 | [小于 K 的两数之和](https://mp.weixin.qq.com/s/S6BbLeP_th_9JheNX7NN-w) | |
## 几篇学习算法的经验贴
[六千字干货文:到底要怎么去学算法?](https://mp.weixin.qq.com/s/7cpixzxE2DLaEn7F615AqQ)
[微信大佬总结的算法学习经验](https://mp.weixin.qq.com/s/fECqsr3T4WKNcx7s-2ozuA)
邮箱:leetcodeanimation@qq.com
喜欢就 star❤️ 一下吧!
## 和我交流
![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/6nqpz.jpeg)
![](Pictures/qrcode.jpg)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment