From 24c2458f4f9c195220e11e9d2630179cffe9e94d Mon Sep 17 00:00:00 2001 From: whai Date: Tue, 23 Jan 2024 22:09:57 +0800 Subject: [PATCH] xc --- .../whaifree/leetCode/Tree/LeetCode100.java | 81 ++++++++ .../whaifree/leetCode/Tree/LeetCode257.java | 173 ++++++++++++++++++ 2 files changed, 254 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode100.java create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode257.java diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode100.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode100.java new file mode 100644 index 0000000..0f51758 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode100.java @@ -0,0 +1,81 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/23 19:56 + * @注释 + */ +public class LeetCode100 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1,2,1}); + TreeNode treeNode1 = TreeNode.constructTree(new Integer[]{1,1,2}); + treeNode1.printTree(); + treeNode.printTree(); + System.out.println(new Solution1().isSameTree(treeNode, treeNode1)); + + } + + class Solution { + public boolean isSameTree(TreeNode p, TreeNode q) { + if (p == null && q == null) { + return true; + } else if (p == null || q == null) { + return false; + } + // LinkedList能存null + Deque deque = new LinkedList<>(); + deque.add(p); + deque.add(q); + while (!deque.isEmpty()) { + // 防止只剩下一个元素,如果只剩下一个元素,那么一定是false + TreeNode BPop = deque.pop(); + TreeNode APop = deque.pop(); + if (BPop == null && APop == null) { + // 叶子节点,不需要加入左右子节点 + continue; + } else if (BPop != null && APop == null) { + return false; + } else if (BPop == null && APop != null) { + return false; + } else if (BPop.val != APop.val) { + return false; + } + + deque.add(BPop.right); + deque.add(APop.right); + deque.add(BPop.left); + deque.add(APop.left); + } + return true; + } + + } + class Solution1 { + public boolean isSameTree(TreeNode p, TreeNode q) { + + if (p == null && q == null) { + return true; + } else if (p == null && q != null) { + return false; + } else if (p != null && q == null) { + return false; + } else if (p.val != q.val) { + return false; + } + boolean sameTreeRight = isSameTree(p.right, q.right); + boolean sameTreeLeft = isSameTree(p.left, q.left); + return sameTreeLeft && sameTreeRight; + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode257.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode257.java new file mode 100644 index 0000000..e3db3a3 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode257.java @@ -0,0 +1,173 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.Node; +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/22 21:54 + * @注释 + */ +public class LeetCode257 { + + @Test + public void test() { + + TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 5,6,7,8,9,0,6}); + root.printTree(); + + System.out.println(new Solution3().binaryTreePaths(root)); + } + class Solution { + + List res = new ArrayList(); + + public List binaryTreePaths(TreeNode root) { + if (root == null) { + return res; + } + deal(root, ""); + return res; + } + + public void deal(TreeNode root, String s) { + + if (root == null) return; + if (root.left == null && root.right == null) { + res.add(new StringBuilder(s).append(root.val).toString()); + } + String tmp = new StringBuilder(s).append(root.val).append("->").toString(); + deal(root.left, tmp); + deal(root.right, tmp); + } + + } + + + class Solution1 { + + List res = new ArrayList(); + + public List binaryTreePaths(TreeNode root) { + if (root == null) { + return res; + } + deal(root, ""); + return res; + } + + public void deal(TreeNode treeNode, String s) { + + if (treeNode==null) return; + //根节点 + if (treeNode.right == null && treeNode.left == null) { + res.add(s + treeNode.val); +// res.add(new StringBuilder().append(s).append(treeNode.val).toString()); + } + +// String tmp = new StringBuilder().append(s).append("->").append(treeNode.val).toString(); + String tmp = s + treeNode.val + "->"; + deal(treeNode.left, tmp); + deal(treeNode.right, tmp); + } + + + + } + + class Solution2 { + + List res = new ArrayList(); + List route = new ArrayList(); + + + public List binaryTreePaths(TreeNode root) { + if (root == null) { + return res; + } + deal(root); + return res; + } + + public void deal(TreeNode treeNode) { + if (treeNode == null) { + return; + } + route.add(treeNode.val); + if (treeNode.left == null && treeNode.right == null) { + // 根节点 + // 可以知道结果了 + StringBuilder stringBuilder = new StringBuilder(); + int size = route.size(); + for (int i = 0; i < size - 1; i++) { + stringBuilder.append(route.get(i)).append("->"); + } + stringBuilder.append(route.get(size - 1)); + res.add(stringBuilder.toString()); + return; + } + if (treeNode.right != null) { + deal(treeNode.right); + // 回溯 + route.remove(route.size() - 1); + } + if (treeNode.left != null) { + deal(treeNode.left); + route.remove(route.size() - 1); + } + + } + + + + } + + class Solution3 { + + + public List binaryTreePaths(TreeNode root) { + List res = new ArrayList(); + + if (root == null) { + return res; + } + + Deque stack = new ArrayDeque<>(); + stack.add(root); + stack.add(root.val + ""); + while (!stack.isEmpty()) { + TreeNode nodePeek = (TreeNode) stack.pop(); + String s = (String) stack.pop(); + // 叶子节点 + if (nodePeek.left == null && nodePeek.right == null) { +// s += "->" + nodePeek.val; + res.add(s); + } + + // 左右进栈 + if (nodePeek.left != null) { + stack.add(nodePeek.left); + stack.add(new StringBuilder().append(s).append("->").append(nodePeek.left.val).toString()); + } + if (nodePeek.right != null) { + stack.add(nodePeek.right); + stack.add(new StringBuilder().append(s).append("->").append(nodePeek.right.val).toString()); + } + } + + return res; + } + + + + } + + +}