From a3f6650e10b9d0ae3835d1be1636d437d5b781c8 Mon Sep 17 00:00:00 2001 From: whai Date: Tue, 16 Jan 2024 21:09:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=9A=84=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95=20=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/Tree/LeetCode102.java | 50 +++++++ .../cn/whaifree/leetCode/Tree/LeetCode94.java | 135 +++++++++++++++++- 2 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java new file mode 100644 index 0000000..55d3df4 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java @@ -0,0 +1,50 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/16 20:37 + * @注释 + */ +public class LeetCode102 { + + + @Test + public void test() { + System.out.println(new Solution().levelOrder(TreeNode.constructTree(new Integer[]{3,9,20,null,null,15,7}))); + } + + class Solution { + public List> levelOrder(TreeNode root) { + + List> res = new LinkedList<>(); + if (root == null) { + return res; + } + Deque queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + // 遍历本层的个数 + List e = new ArrayList<>(); + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode pop = queue.pop(); + e.add(pop.val); + if(pop.left!=null) queue.add(pop.left); + if(pop.right!=null) queue.add(pop.right); + } + res.add(e); + } + + return res; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java index 3f2c5ef..0261ecd 100644 --- a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java @@ -4,10 +4,7 @@ import cn.whaifree.leetCode.model.ListNode; import cn.whaifree.leetCode.model.TreeNode; import org.junit.Test; -import java.util.ArrayList; -import java.util.Deque; -import java.util.LinkedList; -import java.util.List; +import java.util.*; /** * @version 1.0 @@ -19,10 +16,14 @@ public class LeetCode94 { @Test public void test() { - TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5}); + TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4}); TreeNode.printTree(root); - System.out.println(new Solution1().inorderTraversal(root)); + System.out.println(new Solution2().inorderTraversal(root)); + System.out.println(new Solution2().preorderTraversal(root)); + System.out.println(new Solution2().postorderTraversal(root)); + + } class Solution { @@ -75,4 +76,126 @@ public class LeetCode94 { } } + + class Solution2 { + + + /** + * 不能 前后序统一 + * 因为中间节点是在中间,每次经过他都不能判断是否是第一次进入 + * - 如果增加一个标记,标记Null入栈,判断到null时就证明他是已经走过的节点 + * @param root + * @return + */ + public List inorderTraversal(TreeNode root) { + + List list = new LinkedList (); + if (root == null) { + return list; + } + Deque stack = new LinkedList<>(); + + stack.push(root); + /** + * 右边进栈,再左边进栈,中间用空指针标记 + */ + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + // 这个if处理中间节点是否处理过 + if (pop != null) { + if(pop.right!=null) stack.push(pop.right); + // 此处null 为标记已经处理过的节点,下次遇到这个节点就可以直接输出了 + stack.push(pop); + stack.push(null); + if (pop.left!=null) stack.push(pop.left); + } else { + TreeNode pop1 = stack.pop(); + list.add(pop1.val); + } + + } + return list; + } + + + /** + * 不能 前后序统一 + * 因为中间节点是在中间,每次经过他都不能判断是否是第一次进入 + * - 如果增加一个标记,标记Null入栈,判断到null时就证明他是已经走过的节点 + * @param root + * @return + */ + public List preorderTraversal(TreeNode root) { + + List list = new LinkedList (); + if (root == null) { + return list; + } + Deque stack = new LinkedList<>(); + + stack.push(root); + /** + * 右边进栈,再左边进栈,中间用空指针标记 + */ + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + + if (pop != null) { + // 变更这三个的顺序就能 前中后序 + if (pop.right != null) { + stack.push(pop.right); + } + if (pop.left != null) { + stack.push(pop.left); + } + stack.push(pop); + stack.push(null); + } else { + TreeNode pop1 = stack.pop(); + list.add(pop1.val); + } + + } + return list; + } + + public List postorderTraversal(TreeNode root) { + + List list = new LinkedList (); + if (root == null) { + return list; + } + Deque stack = new LinkedList<>(); + + stack.push(root); + /** + * 右边进栈,再左边进栈,中间用空指针标记 + */ + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + + if (pop != null) { + + stack.push(pop); + stack.push(null); + if (pop.right != null) { + stack.push(pop.right); + } + if (pop.left != null) { + stack.push(pop.left); + } + + } else { + TreeNode pop1 = stack.pop(); + list.add(pop1.val); + } + + } + return list; + + } + + } + + }