力扣:103:二叉树的锯齿形层序遍历(未完成)
This commit is contained in:
parent
cdb1ce829e
commit
ff2c9408d1
@ -1,6 +1,8 @@
|
||||
package com.code.leet.entiy;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: hyy
|
||||
* @Date: 2020-02-13 18:25
|
||||
@ -22,4 +24,8 @@ public class TreeNode {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public TreeNode(List<Integer> list){
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
}
|
@ -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>
|
@ -1,60 +0,0 @@
|
||||

|
||||
|
||||
---
|
||||
|
||||
### 🧠 解题思路
|
||||
|
||||
我们解决这道题的关键在于,需要知道哪些是需要去除的外层括号,为了找到这些需要去除的外层括号,我们可以使用到计数器。
|
||||
|
||||
**规则:** 遇到左括号,我们的计数器 *+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* ,则属于有效的右括号。
|
||||
|
||||
---
|
||||
|
||||
### 🎨 图解演示
|
||||
|
||||
        
|
||||
|
||||
---
|
||||
|
||||
### 🍭 示例代码
|
||||
|
||||
```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 📑 多多益善~
|
||||
|
||||
⛲⛲⛲ 期待下次再见~
|
@ -1,48 +0,0 @@
|
||||

|
||||
|
||||
---
|
||||
|
||||
### 🧠 解题思路
|
||||
|
||||
分析题意之后,可以得出以下结论:
|
||||
|
||||
1. 字符要做比较,所以之前的字符应该被存储下来,这里我们会用到栈。
|
||||
2. 遍历字符,若栈顶和当前字符正好大小写都具备,则弹出栈顶抵消,否则当前字符入栈。
|
||||
|
||||
---
|
||||
|
||||
### 🎨 图解演示
|
||||
|
||||
     
|
||||
|
||||
---
|
||||
|
||||
### 🍭 示例代码
|
||||
|
||||
```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 📑 多多益善~
|
||||
|
||||
⛲⛲⛲ 期待下次再见~
|
Loading…
Reference in New Issue
Block a user