From 3247b3f65492db733e4e5d1a2171b51a7b6a200e Mon Sep 17 00:00:00 2001 From: whai Date: Fri, 19 Jan 2024 22:16:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=84=E7=A7=8D=E5=B1=82=E6=AC=A1=E9=81=8D?= =?UTF-8?q?=E5=8E=86=E8=A1=8D=E7=94=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/Tree/LeetCode116.java | 31 +++- .../whaifree/leetCode/Tree/LeetCode226.java | 148 ++++++++++++++++++ 2 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode226.java diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode116.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode116.java index dd15b9b..156b393 100644 --- a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode116.java +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode116.java @@ -51,14 +51,11 @@ public class LeetCode116 { node.left.right = new Node(5); node.right.left = new Node(6); node.right.right = new Node(7); - System.out.println(new Solution().connect(node)); + System.out.println(new Solution1().connect(node)); } class Solution { - - - public Node connect(Node root) { if (root == null) { return null; @@ -89,4 +86,30 @@ public class LeetCode116 { } } + + class Solution1 { + + /** + * 递归方法 + * @param root + * @return + */ + public Node connect(Node root) { + if (root == null) { + return null; + } + circle(root); + return root; + } + + void circle(Node root) { + if (root == null) { + return; + } + if (root.left!=null) root.left.next = root.right == null ? null : root.right; + if (root.right!=null) root.right.next = root.next == null ? null : root.next.left; + circle(root.left); + circle(root.right); + } + } } diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode226.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode226.java new file mode 100644 index 0000000..f7f8182 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode226.java @@ -0,0 +1,148 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/19 14:54 + * @注释 + */ +public class LeetCode226 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5}); + treeNode.printTree(); + TreeNode treeNode1 = new Solution2().invertTree(treeNode); + treeNode1.printTree(); + } + + class Solution { + public TreeNode invertTree(TreeNode root) { + reverse(root); + return root; + } + + public void reverse(TreeNode root) { + if (root == null) { + return; + } + swap(root); + invertTree(root.left); + invertTree(root.right); + } + + public void swap(TreeNode root) { + TreeNode tmp = root.left; + root.left = root.right; + root.right = tmp; + } + } + + class Solution1 { + /** + * 层次遍历 + * @param root + * @return + */ + public TreeNode invertTree(TreeNode root) { + if (root == null) { + return root; + } + Deque queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + TreeNode pop = queue.pop(); + swap(pop); + if (pop.right != null) { + queue.add(pop.right); + } + if (pop.left != null) { + queue.add(pop.left); + } + } + return root; + } + + + public void swap(TreeNode root) { + TreeNode tmp = root.left; + root.left = root.right; + root.right = tmp; + } + } + + class Solution2 { + /** + * 先序遍历 + * @param root + * @return + */ + public TreeNode invertTree(TreeNode root) { + if (root == null) { + return root; + } + Deque stack = new LinkedList<>(); + stack.push(root); + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + swap(pop); + if (pop.right != null) { + stack.push(pop.right); + } + if (pop.left != null) { + stack.push(pop.left); + } + + } + return root; + } + + + public void swap(TreeNode root) { + TreeNode tmp = root.left; + root.left = root.right; + root.right = tmp; + } + } + + class Solution3 { + /** + * 先序遍历 + * @param root + * @return + */ + public TreeNode invertTree(TreeNode root) { + if (root == null) { + return root; + } + Deque stack = new LinkedList<>(); + stack.push(root); + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + swap(pop); + if (pop.right != null) { + stack.push(pop.right); + } + if (pop.left != null) { + stack.push(pop.left); + } + + } + return root; + } + + + public void swap(TreeNode root) { + TreeNode tmp = root.left; + root.left = root.right; + root.right = tmp; + } + } + +}