力扣:103:二叉树的锯齿形层序遍历(未完成)

This commit is contained in:
huangge1199 2021-04-01 17:10:10 +08:00
parent cdb1ce829e
commit ff2c9408d1
5 changed files with 126 additions and 108 deletions

View File

@ -1,6 +1,8 @@
package com.code.leet.entiy; package com.code.leet.entiy;
import java.util.List;
/** /**
* @Author: hyy * @Author: hyy
* @Date: 2020-02-13 18:25 * @Date: 2020-02-13 18:25
@ -22,4 +24,8 @@ public class TreeNode {
this.left = left; this.left = left;
this.right = right; this.right = right;
} }
public TreeNode(List<Integer> list){
}
} }

View File

@ -0,0 +1,97 @@
//给定一个二叉树返回其节点值的锯齿形层序遍历即先从左往右再从右往左进行下一层遍历以此类推层与层之间交替进行
//
// 例如
//给定二叉树 [3,9,20,null,null,15,7],
//
//
// 3
// / \
// 9 20
// / \
// 15 7
//
//
// 返回锯齿形层序遍历如下
//
//
//[
// [3],
// [20,9],
// [15,7]
//]
//
// Related Topics 广度优先搜索
// 👍 421 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
//103:二叉树的锯齿形层序遍历
public class BinaryTreeZigzagLevelOrderTraversal {
public static void main(String[] args) {
//测试代码
Solution solution = new BinaryTreeZigzagLevelOrderTraversal().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(root);
TreeNode temp;
List<Integer> list;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
list = new ArrayList<>();
while (!stack1.isEmpty()) {
temp = stack1.pop();
list.add(temp.val);
if (temp.left != null) {
stack2.push(temp.left);
}
if (temp.right != null) {
stack2.push(temp.right);
}
result.add(list);
}
list = new ArrayList<>();
while (!stack2.isEmpty()) {
temp = stack2.pop();
list.add(temp.val);
if (temp.right != null) {
stack1.push(temp.right);
}
if (temp.left != null) {
stack1.push(temp.left);
}
result.add(list);
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,23 @@
<p>给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。</p>
<p>例如:<br />
给定二叉树 <code>[3,9,20,null,null,15,7]</code>,</p>
<pre>
3
/ \
9 20
/ \
15 7
</pre>
<p>返回锯齿形层序遍历如下:</p>
<pre>
[
[3],
[20,9],
[15,7]
]
</pre>
<div><div>Related Topics</div><div><li></li><li></li><li>广度优先搜索</li></div></div>\n<div><li>👍 421</li><li>👎 0</li></div>

View File

@ -1,60 +0,0 @@
![图解每日一练.jpg](https://pic.leetcode-cn.com/1615817903-fzmpwZ-%E5%9B%BE%E8%A7%A3%E6%AF%8F%E6%97%A5%E4%B8%80%E7%BB%83.jpg)
---
### 🧠 解题思路
我们解决这道题的关键在于,需要知道哪些是需要去除的外层括号,为了找到这些需要去除的外层括号,我们可以使用到计数器。
**规则:** 遇到左括号,我们的计数器 *+1*,遇到右括号,我们的计数器 *-1*
这样的话,一组连续且有效的括号,将不会对计数器的值产生变化。
```js
// 示例一
当前的计数值: 0 1 0 1
( ) ( )
遍历后计数值: 1 0 1 0
// 示例二
当前的计数值: 0 1 2 1 2 1 0 1
( ( ) ( ) ) ( )
遍历后计数值: 1 2 1 2 1 0 1 0
```
根据上述两个示例,我们可以很快的找出规律:
1. 遇到左括号,当前计数值大于 *0* ,则属于有效的左括号。
2. 遇到右括号,当前计数值大于 *1* ,则属于有效的右括号。
---
### 🎨 图解演示
![1.jpg](https://pic.leetcode-cn.com/1615909098-eOohaJ-1.jpg) ![2.jpg](https://pic.leetcode-cn.com/1615908912-aGpYJn-2.jpg) ![3.jpg](https://pic.leetcode-cn.com/1615908914-oGzkUH-3.jpg) ![4.jpg](https://pic.leetcode-cn.com/1615908917-mGekYh-4.jpg) ![5.jpg](https://pic.leetcode-cn.com/1615908919-mQrLwp-5.jpg) ![6.jpg](https://pic.leetcode-cn.com/1615908921-ZUDUic-6.jpg) ![7.jpg](https://pic.leetcode-cn.com/1615908923-AKfYyO-7.jpg) ![8.jpg](https://pic.leetcode-cn.com/1615908926-zzQCRy-8.jpg) ![9.jpg](https://pic.leetcode-cn.com/1615908928-KccJnw-9.jpg)
---
### 🍭 示例代码
```Javascript []
var removeOuterParentheses = function(S) {
let count = 0, ans = '';
for (let i = 0; i < S.length; i++) {
if(S[i] === '(' && count++ > 0) ans += '('
if(S[i] === ')' && count-- > 1) ans += ')';
}
return ans;
};
```
---
### 转身挥手
嘿,少年,做图不易,留下个赞或评论再走吧!谢啦~ 💐
差点忘了,祝你牛年大吉 🐮 AC 和 Offer 📑 多多益善~
⛲⛲⛲ 期待下次再见~

View File

@ -1,48 +0,0 @@
![图解每日一练.jpg](https://pic.leetcode-cn.com/1615817903-fzmpwZ-%E5%9B%BE%E8%A7%A3%E6%AF%8F%E6%97%A5%E4%B8%80%E7%BB%83.jpg)
---
### 🧠 解题思路
分析题意之后,可以得出以下结论:
1. 字符要做比较,所以之前的字符应该被存储下来,这里我们会用到栈。
2. 遍历字符,若栈顶和当前字符正好大小写都具备,则弹出栈顶抵消,否则当前字符入栈。
---
### 🎨 图解演示
![1.jpg](https://pic.leetcode-cn.com/1616513770-cbkAnG-1.jpg) ![2.jpg](https://pic.leetcode-cn.com/1616513772-ffshlQ-2.jpg) ![3.jpg](https://pic.leetcode-cn.com/1616513774-oJkAFT-3.jpg) ![4.jpg](https://pic.leetcode-cn.com/1616513777-jpPCEP-4.jpg) ![5.jpg](https://pic.leetcode-cn.com/1616513779-LcycBt-5.jpg) ![6.jpg](https://pic.leetcode-cn.com/1616513781-XeIFCV-6.jpg)
---
### 🍭 示例代码
```Javascript []
var makeGood = function(s) {
let res = [];
for(let i of s){
if(
res.length &&
res[res.length - 1] !== i &&
res[res.length - 1].toUpperCase() === i.toUpperCase()
){
res.pop();
}else{
res.push(i);
}
}
return res.join("");
};
```
---
### 转身挥手
嘿,少年,做图不易,留下个赞或评论再走吧!谢啦~ 💐
差点忘了,祝你牛年大吉 🐮 AC 和 Offer 📑 多多益善~
⛲⛲⛲ 期待下次再见~