diff --git a/LeetCode/src/main/java/com/code/leet/entiy/TreeNode.java b/LeetCode/src/main/java/com/code/leet/entiy/TreeNode.java index f06fb34..72606f2 100644 --- a/LeetCode/src/main/java/com/code/leet/entiy/TreeNode.java +++ b/LeetCode/src/main/java/com/code/leet/entiy/TreeNode.java @@ -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 list){ + + } } diff --git a/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeZigzagLevelOrderTraversal.java b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeZigzagLevelOrderTraversal.java new file mode 100644 index 0000000..705650c --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeZigzagLevelOrderTraversal.java @@ -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> zigzagLevelOrder(TreeNode root) { + List> result = new ArrayList<>(); + Stack stack1 = new Stack<>(); + Stack stack2 = new Stack<>(); + stack1.push(root); + TreeNode temp; + List 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) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeZigzagLevelOrderTraversal.md b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeZigzagLevelOrderTraversal.md new file mode 100644 index 0000000..c32d4c5 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/BinaryTreeZigzagLevelOrderTraversal.md @@ -0,0 +1,23 @@ +

给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

+ +

例如:
+给定二叉树 [3,9,20,null,null,15,7],

+ +
+    3
+   / \
+  9  20
+    /  \
+   15   7
+
+ +

返回锯齿形层序遍历如下:

+ +
+[
+  [3],
+  [20,9],
+  [15,7]
+]
+
+
Related Topics
  • 广度优先搜索
  • \n
  • 👍 421
  • 👎 0
  • \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/doc/ji-shu-fa-shan-chu-zui-wai-ceng-de-gua-h-55id.md b/LeetCode/src/main/java/leetcode/editor/cn/doc/ji-shu-fa-shan-chu-zui-wai-ceng-de-gua-h-55id.md deleted file mode 100644 index 5b68345..0000000 --- a/LeetCode/src/main/java/leetcode/editor/cn/doc/ji-shu-fa-shan-chu-zui-wai-ceng-de-gua-h-55id.md +++ /dev/null @@ -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 📑 多多益善~ - -⛲⛲⛲ 期待下次再见~ \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/doc/zhan-zheng-li-zi-fu-chuan-by-demigodliu-5xwr.md b/LeetCode/src/main/java/leetcode/editor/cn/doc/zhan-zheng-li-zi-fu-chuan-by-demigodliu-5xwr.md deleted file mode 100644 index dc27d1e..0000000 --- a/LeetCode/src/main/java/leetcode/editor/cn/doc/zhan-zheng-li-zi-fu-chuan-by-demigodliu-5xwr.md +++ /dev/null @@ -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 📑 多多益善~ - -⛲⛲⛲ 期待下次再见~ \ No newline at end of file