diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6c901b9..4fac1db 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,15 +2,25 @@ - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - + + + + + @@ -85,13 +95,13 @@ - - - @@ -312,9 +330,14 @@ - + + + + @@ -323,62 +346,62 @@ - + - - + + - - + + - - - + + + - - - + + + - - - + + + - - - + + + - + - + - + - + - + - + - + - + - - + + - + diff --git a/src/etc/ali/q4/Info.java b/src/etc/ali/q4/Info.java deleted file mode 100644 index 348d664..0000000 --- a/src/etc/ali/q4/Info.java +++ /dev/null @@ -1,22 +0,0 @@ -package etc.ali.q4; - -public class Info { - private Long userId; - private Long goodsId; - - public Long getGoodsId() { - return goodsId; - } - - public void setGoodsId(Long goodsId) { - this.goodsId = goodsId; - } - - public Long getUserId() { - return userId; - } - - public void setUserId(Long userId) { - this.userId = userId; - } -} diff --git a/src/etc/ali/q4/Info1.java b/src/etc/ali/q4/Info1.java deleted file mode 100644 index aede05e..0000000 --- a/src/etc/ali/q4/Info1.java +++ /dev/null @@ -1,24 +0,0 @@ -package etc.ali.q4; - -import java.util.Date; - -public class Info1 extends Info { - private Date buyTime; - private Double cost; - - public Date getBuyTime() { - return buyTime; - } - - public void setBuyTime(Date buyTime) { - this.buyTime = buyTime; - } - - public Double getCost() { - return cost; - } - - public void setCost(Double cost) { - this.cost = cost; - } -} diff --git a/src/etc/ali/q4/Info2.java b/src/etc/ali/q4/Info2.java deleted file mode 100644 index 12e80e1..0000000 --- a/src/etc/ali/q4/Info2.java +++ /dev/null @@ -1,13 +0,0 @@ -package etc.ali.q4; - -public class Info2 extends Info { - private String address; - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } -} diff --git a/src/etc/ali/q4/Info3.java b/src/etc/ali/q4/Info3.java deleted file mode 100644 index 61fc6eb..0000000 --- a/src/etc/ali/q4/Info3.java +++ /dev/null @@ -1,24 +0,0 @@ -package etc.ali.q4; - -public class Info3 extends Info1 { - private String address; - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - - public Info3() { - } - - public Info3(Info1 info1, Info2 info2) { - this.setUserId(info1.getUserId()); - this.setGoodsId(info1.getGoodsId()); - this.setBuyTime(info1.getBuyTime()); - this.setCost(info1.getCost()); - this.setAddress(info2.getAddress()); - } -} diff --git a/src/etc/ali/q4/Main.java b/src/etc/ali/q4/Main.java deleted file mode 100644 index 1cd6453..0000000 --- a/src/etc/ali/q4/Main.java +++ /dev/null @@ -1,51 +0,0 @@ -package etc.ali.q4; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -public class Main { - - private static List list1 = new ArrayList<>(); - private static List list2 = new ArrayList<>(); - private static List list3 = new ArrayList<>(); - - static { - Info1 info1 = new Info1(); - info1.setUserId(1L); - info1.setGoodsId(1L); - info1.setBuyTime(new Date()); - info1.setCost(20.00); - list1.add(info1); - - Info2 info2 = new Info2(); - info2.setUserId(1L); - info2.setGoodsId(1L); - info2.setAddress("测试"); - list2.add(info2); - } - - public void merge(){ - for (int i = 0; i < list1.size(); i++) { - Long userId = list1.get(i).getUserId(); - Long goodsId = list1.get(i).getGoodsId(); - for (int j = 0; j < list2.size(); j++) { - if (userId.equals(list2.get(j).getUserId()) && goodsId.equals(list2.get(j).getGoodsId())) { - Info3 info3 = new Info3(list1.get(i),list2.get(j)); - list3.add(info3); - } - } - } - } - - public static void main(String[] args) { - new Main().merge(); - for (int i = 0;i stack = new Stack<>(); + while (!stack.empty() || root != null) { + while (root != null) { + System.out.println(root.val); + stack.push(root); + root = root.left; + } + root = stack.pop(); + root = root.right; + } + } + + /** + * 中序遍历 - 递归 + * + * @param root + */ + public void ldr_dg(TreeNode root) { + if (root == null) { + return; + } + ldr_dg(root.left); + System.out.println(root.val); + ldr_dg(root.right); + } + + /** + * 中序遍历 - 非递归 + * + * @param root + */ + public void ldr(TreeNode root) { + Stack stack = new Stack<>(); + while (!stack.empty() || root != null) { + while (root != null) { + stack.push(root); + root = root.left; + } + root = stack.pop(); + System.out.println(root.val); + root = root.right; + } + } + + /** + * 后序遍历 - 递归 + * + * @param root + */ + public void lrd_dg(TreeNode root) { + if (root == null) { + return; + } + lrd_dg(root.left); + lrd_dg(root.right); + System.out.println(root.val); + } + + /** + * 后序遍历 - 非递归 + * + * @param root + */ + public void lrd(TreeNode root) { + if (root == null) { + return; + } + Stack stack = new Stack<>(); + Stack rs = new Stack<>(); + stack.push(root); + + while (!stack.empty()) { + TreeNode temp = stack.pop(); + rs.push(temp.val); + if (temp.left != null) { + stack.push(temp.left); + } + if (temp.right != null) { + stack.push(temp.right); + } + } + + while (!rs.empty()) { + System.out.println(rs.pop()); + } + } + + public static void main(String[] args) { + TreeNode root = new TreeNode(7); + TreeNode n1 = new TreeNode(2); + TreeNode n2 = new TreeNode(3); + root.left = n1; + root.right = n2; + TreeNode n3 = new TreeNode(8); + TreeNode n4 = new TreeNode(6); + TreeNode n5 = new TreeNode(1); + n1.right = n5; + n2.left = n3; + n2.right = n4; + new Main().lrd(null); + +// Deque deque = new LinkedList<>(); +// deque.add(1); +// deque.add(2); +// deque.add(3); +// deque.pollLast(); +// System.out.println(deque); + } +} diff --git a/src/etc/tree/traversal/TreeNode.java b/src/etc/tree/traversal/TreeNode.java new file mode 100644 index 0000000..8e72186 --- /dev/null +++ b/src/etc/tree/traversal/TreeNode.java @@ -0,0 +1,11 @@ +package etc.tree.traversal; + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/src/q144/Solution.java b/src/q144/Solution.java new file mode 100644 index 0000000..ae405e7 --- /dev/null +++ b/src/q144/Solution.java @@ -0,0 +1,34 @@ +package q144; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * 非递归法 o(n) + */ +public class Solution { + public List preorderTraversal(TreeNode root) { + List rs = new ArrayList<>(); + Stack stack = new Stack<>(); + while (!stack.empty() || root != null) { + while (root != null) { + rs.add(root.val); + stack.push(root); + root = root.left; + } + root = stack.pop(); + root = root.right; + } + return rs; + } + + public static void main(String[] args) { + TreeNode root = new TreeNode(1); + TreeNode t1 = new TreeNode(2); + root.right = t1; + TreeNode t2 = new TreeNode(3); + t1.left = t2; + new Solution().preorderTraversal(root); + } +} diff --git a/src/q144/TreeNode.java b/src/q144/TreeNode.java new file mode 100644 index 0000000..00d870c --- /dev/null +++ b/src/q144/TreeNode.java @@ -0,0 +1,11 @@ +package q144; + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/src/q145/Solution.java b/src/q145/Solution.java new file mode 100644 index 0000000..2515aa0 --- /dev/null +++ b/src/q145/Solution.java @@ -0,0 +1,31 @@ +package q145; + +import java.util.LinkedList; +import java.util.List; +import java.util.Stack; + +/** + * 非递归法 o(n) + */ +public class Solution { + public List postorderTraversal(TreeNode root) { + Stack stack = new Stack<>(); + LinkedList rs = new LinkedList<>(); + if (root == null) { + return rs; + } + stack.push(root); + + while (!stack.empty()) { + TreeNode temp = stack.pop(); + rs.addFirst(temp.val); + if (temp.left != null) { + stack.push(temp.left); + } + if (temp.right != null) { + stack.push(temp.right); + } + } + return rs; + } +} diff --git a/src/q145/TreeNode.java b/src/q145/TreeNode.java new file mode 100644 index 0000000..7ba9515 --- /dev/null +++ b/src/q145/TreeNode.java @@ -0,0 +1,11 @@ +package q145; + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/src/q20/Solution.java b/src/q20/Solution.java index 8d63af7..65528ba 100644 --- a/src/q20/Solution.java +++ b/src/q20/Solution.java @@ -2,6 +2,9 @@ package q20; import java.util.Stack; +/** + * 利用栈 o(n) + */ public class Solution { public boolean isValid(String s) { Stack stack = new Stack<>(); diff --git a/src/q94/Solution.java b/src/q94/Solution.java new file mode 100644 index 0000000..7ca4c6d --- /dev/null +++ b/src/q94/Solution.java @@ -0,0 +1,22 @@ +package q94; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class Solution { + public List inorderTraversal(TreeNode root) { + List rs = new ArrayList<>(); + Stack stack = new Stack<>(); + while (!stack.empty() || root != null) { + while (root != null) { + stack.push(root); + root = root.left; + } + root = stack.pop(); + rs.add(root.val); + root = root.right; + } + return rs; + } +} diff --git a/src/q94/TreeNode.java b/src/q94/TreeNode.java new file mode 100644 index 0000000..3d5ead7 --- /dev/null +++ b/src/q94/TreeNode.java @@ -0,0 +1,11 @@ +package q94; + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/src/q98/Solution.java b/src/q98/Solution.java new file mode 100644 index 0000000..fba707b --- /dev/null +++ b/src/q98/Solution.java @@ -0,0 +1,50 @@ +package q98; + +import java.util.ArrayList; +import java.util.List; + +/** + * 递归的中序遍历(二叉搜索树的中序遍历是有序的)o(n) + */ +public class Solution { + + List rs = new ArrayList<>(); + + private List ldr(TreeNode root) { + if (root == null) { + return rs; + } + ldr(root.left); + rs.add(root.val); + ldr(root.right); + return rs; + } + + public boolean isValidBST(TreeNode root) { + ldr(root); + if (rs.size() < 2) { + return true; + } + int a = rs.get(0); + for (int i = 1; i < rs.size(); i++) { + if (rs.get(i) <= a) { + return false; + } + a = rs.get(i); + } + return true; + } + + public static void main(String[] args) { + TreeNode root = new TreeNode(5); + TreeNode n1 = new TreeNode(1); + TreeNode n2 = new TreeNode(4); + root.left = n1; + root.right = n2; + TreeNode n3 = new TreeNode(3); + TreeNode n4 = new TreeNode(6); + n2.left = n3; + n2.right = n4; + System.out.println(new Solution().isValidBST(root)); + } +} diff --git a/src/q98/TreeNode.java b/src/q98/TreeNode.java new file mode 100644 index 0000000..222e309 --- /dev/null +++ b/src/q98/TreeNode.java @@ -0,0 +1,11 @@ +package q98; + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/src/q98/f2/Solution.java b/src/q98/f2/Solution.java new file mode 100644 index 0000000..ca4c92c --- /dev/null +++ b/src/q98/f2/Solution.java @@ -0,0 +1,32 @@ +package q98.f2; + +import java.util.Stack; + +public class Solution { + public boolean isValidBST(TreeNode root) { + Stack stack = new Stack<>(); + while (!stack.empty() || root != null) { + while (root != null) { + stack.push(root); + root = root.left; + } + root = stack.pop(); + System.out.println(root.val); + root = root.right; + } + return true; + } + + public static void main(String[] args) { + TreeNode root = new TreeNode(5); + TreeNode n1 = new TreeNode(1); + TreeNode n2 = new TreeNode(4); + root.left = n1; + root.right = n2; + TreeNode n3 = new TreeNode(3); + TreeNode n4 = new TreeNode(6); + n2.left = n3; + n2.right = n4; + System.out.println(new Solution().isValidBST(root)); + } +} diff --git a/src/q98/f2/TreeNode.java b/src/q98/f2/TreeNode.java new file mode 100644 index 0000000..3ee5d61 --- /dev/null +++ b/src/q98/f2/TreeNode.java @@ -0,0 +1,11 @@ +package q98.f2; + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +}