diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode145.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode145.java new file mode 100644 index 0000000..c968c3d --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode145.java @@ -0,0 +1,77 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/15 20:11 + * @注释 + */ +public class LeetCode145 { + + @Test + public void test() { + TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5}); + + root.printTree(); + new Solution1().postorderTraversal(root).forEach( + System.out::println + ); + } + + class Solution { + List res = new LinkedList<>(); + + public List postorderTraversal(TreeNode root) { + if (root == null) { + return res; + } + postorder(root); + return res; + } + + public void postorder(TreeNode root) { + if (root==null) return; + + postorder(root.left); + postorder(root.right); + res.add(root.val); + + } + } + + + class Solution1 { + public List postorderTraversal(TreeNode root) { + + List res = new LinkedList(); + if (root == null) { + return res; + } + Deque stack = new ArrayDeque<>(); + + stack.push(root); + while (!stack.isEmpty()) { + + TreeNode pop = stack.pop(); + res.add(pop.val); + if (pop.left != null) { + stack.push(pop.left); + } + if (pop.right != null) { + stack.push(pop.right); + } + } + + Collections.reverse(res); + + return res; + } + + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java new file mode 100644 index 0000000..3f2c5ef --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode94.java @@ -0,0 +1,78 @@ +package cn.whaifree.leetCode.Tree; + +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; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/15 19:29 + * @注释 + */ +public class LeetCode94 { + + @Test + public void test() { + TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5}); + TreeNode.printTree(root); + + System.out.println(new Solution1().inorderTraversal(root)); + } + + class Solution { + List res = new LinkedList<>(); + + public List inorderTraversal(TreeNode root) { + if (root == null) { + return res; + } + inorder(root); + return res; + } + + public void inorder(TreeNode root) { + if (root == null) { + return; + } + inorder(root.left); + res.add(root.val); + inorder(root.right); + } + + } + + class Solution1 { + + + public List inorderTraversal(TreeNode root) { + + List list = new LinkedList (); + if (root == null) { + return list; + } + Deque stack = new LinkedList<>(); + + TreeNode index = root; + while (!stack.isEmpty() || index != null) { + // 不断左边加入,弹出获取右边的 + // 每次循环处理一次节点,左边,右边和中间 + if (index != null) { + stack.push(index); + index = index.left; + } else { + index = stack.pop(); + list.add(index.val); + index = index.right; + } + } + return list; + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java index 1f17f05..d66d9d1 100644 --- a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java +++ b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java @@ -55,6 +55,10 @@ public class TreeNode { printNodeInternal(Collections.singletonList(root), 1, maxLevel); } + public void printTree() { + printTree(this); + } + private static void printNodeInternal(List nodes, int level, int maxLevel) { if (nodes == null || nodes.isEmpty() || isAllElementsNull(nodes)) { return;