Skip to content

Commit c26c184

Browse files
committed
20181230
1 parent 9e141c3 commit c26c184

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

code/lc144.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package code;
2+
/*
3+
* 144. Binary Tree Preorder Traversal
4+
* 题意:二叉树先序遍历
5+
* 难度:Medium
6+
* 分类:Stack, Tree
7+
* 思路:左节点依次入栈
8+
* Tips:和lc94中序,lc145后序一起看
9+
*/
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.Stack;
13+
14+
public class lc144 {
15+
public class TreeNode {
16+
int val;
17+
TreeNode left;
18+
TreeNode right;
19+
TreeNode(int x) { val = x; }
20+
}
21+
public List<Integer> inorderTraversal(TreeNode root) {
22+
List<Integer> res = new ArrayList<>();
23+
if(root==null)
24+
return res;
25+
Stack<TreeNode> st = new Stack();
26+
while( !st.isEmpty() || root!=null ) { //注意停止条件
27+
while (root != null) {
28+
st.push(root);
29+
res.add(root.val);
30+
root = root.left;
31+
}
32+
root = st.pop();
33+
root = root.right;
34+
}
35+
return res;
36+
}
37+
}

code/lc145.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package code;
2+
/*
3+
* 145. Binary Tree Postorder Traversal
4+
* 题意:二叉树后序遍历
5+
* 难度:Hard
6+
* 分类:Stack, Tree
7+
* 思路:调整的先序遍历,再反转结果
8+
*/
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import java.util.Stack;
13+
14+
public class lc145 {
15+
public class TreeNode {
16+
int val;
17+
TreeNode left;
18+
TreeNode right;
19+
TreeNode(int x) { val = x; }
20+
}
21+
public List<Integer> postorderTraversal(TreeNode root) {
22+
ArrayList<Integer> res = new ArrayList();
23+
if(root==null)
24+
return res;
25+
Stack<TreeNode> st = new Stack();
26+
while(!st.isEmpty()||root!=null){
27+
while(root!=null) {
28+
st.add(root);
29+
res.add(root.val);
30+
root = root.right; //先遍历右节点
31+
}
32+
root = st.pop();
33+
root =root.left; //再左节点
34+
}
35+
Collections.reverse(res); //反转链表
36+
return res;
37+
}
38+
}

code/lc94.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package code;
2+
/*
3+
* 94. Binary Tree Inorder Traversal
4+
* 题意:二叉树中序遍历
5+
* 难度:Medium
6+
* 分类:HashTable, Stack, Tree
7+
* 思路:左节点依次入栈
8+
* Tips:和lc144前序,lc145后序一起看
9+
*/
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.Stack;
13+
14+
public class lc94 {
15+
public class TreeNode {
16+
int val;
17+
TreeNode left;
18+
TreeNode right;
19+
TreeNode(int x) { val = x; }
20+
}
21+
public List<Integer> inorderTraversal(TreeNode root) {
22+
List<Integer> res = new ArrayList<>();
23+
if(root==null)
24+
return res;
25+
Stack<TreeNode> st = new Stack();
26+
while( !st.isEmpty() || root!=null ) { //注意停止条件
27+
while (root != null) {
28+
st.push(root);
29+
root = root.left;
30+
}
31+
root = st.pop();
32+
res.add(root.val);
33+
root = root.right;
34+
}
35+
return res;
36+
}
37+
}

code/lc96.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package code;
2+
/*
3+
* 96. Unique Binary Search Trees
4+
* 题意:给定n,多少棵二叉搜索树,1~n
5+
* 难度:Medium
6+
* 分类:Dynamic Programming, Tree
7+
* 思路:1~n都可以做根节点,分成左右两个子问题 dp[i] * dp[n-1-i]
8+
* Tips:
9+
*/
10+
public class lc96 {
11+
public static void main(String[] args) {
12+
System.out.println(numTrees(3));
13+
}
14+
public static int numTrees(int n) {
15+
if(n<2)
16+
return 1;
17+
int[] dp = new int[n+1];
18+
dp[0] = 1;
19+
dp[1] = 1;
20+
for (int i = 2; i < n+1; i++) {
21+
for (int j = 0; j <i ; j++) {
22+
dp[i] += dp[j] * dp[i-1-j];
23+
}
24+
}
25+
return dp[n];
26+
}
27+
}

code/lc98.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package code;
2+
/*
3+
* 98. Validate Binary Search Tree
4+
* 题意:判断是否为二叉搜索树
5+
* 难度:Medium
6+
* 分类:Tree, Depth-first Search
7+
* 思路:两种方法,一种递归;另一种中序遍历的思路;
8+
* Tips:递归时注意设置最大最小两个参数,因为节点间val限制会传递的
9+
*/
10+
import java.util.Stack;
11+
12+
public class lc98 {
13+
public static void main(String[] args) {
14+
TreeNode tn1 = new TreeNode(0);
15+
tn1.left = new TreeNode(-1);
16+
//System.out.println(isValidBST(tn1));
17+
System.out.println(isValidBST2(tn1));
18+
}
19+
public static class TreeNode {
20+
int val;
21+
TreeNode left;
22+
TreeNode right;
23+
TreeNode(int x) { val = x; }
24+
}
25+
public static boolean isValidBST(TreeNode root) {
26+
if(root==null)
27+
return true;
28+
return dfs(root, -Double.MAX_VALUE, Double.MAX_VALUE); // 注意Double.MIN_VALUE是接近0的正数
29+
}
30+
public static boolean dfs(TreeNode root, double min_bound, double max_bound){ //设置两个参数,一个最大值,一个最小值
31+
if (root == null) return true;
32+
if (root.val >= max_bound || root.val <= min_bound) return false;
33+
return dfs(root.left, min_bound, root.val) && dfs(root.right, root.val, max_bound);
34+
}
35+
36+
public static boolean isValidBST2(TreeNode root) {
37+
if(root == null)
38+
return true;
39+
Stack<TreeNode> st = new Stack();
40+
TreeNode pre = null;
41+
while( !st.isEmpty() || root!=null ){
42+
while(root!=null){
43+
st.add(root);
44+
root = root.left;
45+
}
46+
root = st.pop();
47+
if( pre!=null && root.val<=pre.val ) //先序遍历,自下而上,孩子节点小于父节点
48+
return false;
49+
pre = root;
50+
root = root.right;
51+
}
52+
return true;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)