Skip to content

Commit 1835ab1

Browse files
author
chenweijie
committed
验证二叉树 二叉树的公共祖先
1 parent c71bbfc commit 1835ab1

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.chen.algorithm.study.test235;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-08-30 23:13
6+
*/
7+
public class Solution {
8+
9+
10+
class TreeNode {
11+
int val;
12+
TreeNode left;
13+
TreeNode right;
14+
15+
TreeNode(int x) {
16+
val = x;
17+
}
18+
}
19+
20+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
21+
22+
if (p.val < root.val && q.val < root.val) {
23+
return lowestCommonAncestor(root.left, p, q);
24+
}
25+
26+
if (p.val > root.val && q.val > root.val) {
27+
return lowestCommonAncestor(root.right, p, q);
28+
}
29+
30+
return root;
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen.algorithm.study.test235;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-08-30 23:02
6+
*/
7+
public class Solution2 {
8+
9+
class TreeNode {
10+
int val;
11+
TreeNode left;
12+
TreeNode right;
13+
14+
TreeNode(int x) {
15+
val = x;
16+
}
17+
}
18+
19+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
20+
21+
while (root != null) {
22+
23+
if (p.val < root.val && q.val < root.val) {
24+
root = root.left;
25+
} else if (p.val > root.val && q.val > root.val) {
26+
root = root.right;
27+
} else {
28+
return root;
29+
}
30+
}
31+
return root;
32+
}
33+
34+
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.chen.algorithm.study.test236;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-08-30 23:02
6+
*/
7+
public class Solution {
8+
9+
class TreeNode {
10+
int val;
11+
TreeNode left;
12+
TreeNode right;
13+
14+
TreeNode(int x) {
15+
val = x;
16+
}
17+
}
18+
19+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
20+
21+
if (root == null) {
22+
return root;
23+
}
24+
25+
if (root == p || root == q) {
26+
return root;
27+
}
28+
29+
TreeNode left = lowestCommonAncestor(root.left, p, q);
30+
TreeNode right = lowestCommonAncestor(root.right, p, q);
31+
32+
if (left == null) {
33+
return right;
34+
}
35+
36+
if (right == null) {
37+
return left;
38+
}
39+
40+
return root;
41+
}
42+
43+
44+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.chen.algorithm.study.test98;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-08-30 18:32
6+
*/
7+
public class Solution {
8+
9+
10+
class TreeNode {
11+
int val;
12+
TreeNode left;
13+
TreeNode right;
14+
15+
TreeNode(int x) {
16+
val = x;
17+
}
18+
}
19+
20+
21+
/**
22+
* 要求根节点大于左子树的最大值,小于右子树的最小值
23+
*
24+
* @param root
25+
* @return
26+
*/
27+
public boolean isValidBST(TreeNode root) {
28+
return isValid(root, null, null);
29+
}
30+
31+
32+
public boolean isValid(TreeNode root, Integer max, Integer min) {
33+
34+
if (root == null) {
35+
return true;
36+
}
37+
if (min != null && root.val <= min) {
38+
return false;
39+
}
40+
if (max != null && root.val >= max) {
41+
return false;
42+
}
43+
44+
return isValid(root.left, min, root.val) && isValid(root.right, root.val, max);
45+
}
46+
47+
48+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.chen.algorithm.study.test98;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author : chen weijie
7+
* @Date: 2020-08-30 20:54
8+
*/
9+
public class Solution1 {
10+
11+
12+
class TreeNode {
13+
int val;
14+
TreeNode left;
15+
TreeNode right;
16+
17+
TreeNode(int x) {
18+
val = x;
19+
}
20+
}
21+
22+
23+
/**
24+
* 要求根节点大于左子树的最大值,小于右子树的最小值
25+
*
26+
* @param root
27+
* @return
28+
*/
29+
public boolean isValidBST(TreeNode root) {
30+
31+
if (root == null) {
32+
return true;
33+
}
34+
35+
Stack<TreeNode> stack = new Stack<>();
36+
TreeNode curr = root;
37+
38+
// int preVal = Integer.MIN_VALUE;
39+
double preVal = -Double.MAX_VALUE;
40+
while (!stack.isEmpty() || curr != null) {
41+
while (curr != null) {
42+
stack.push(curr);
43+
curr = curr.left;
44+
}
45+
46+
curr = stack.pop();
47+
int currVal = curr.val;
48+
if (preVal >= currVal) {
49+
return false;
50+
}
51+
preVal = currVal;
52+
curr = curr.right;
53+
}
54+
return true;
55+
}
56+
57+
58+
59+
60+
61+
}

0 commit comments

Comments
 (0)