forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Jun 22, 2019
1 parent
f71391f
commit a69f402
Showing
8 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
lab-09/src/main/java/cn/iocoder/springboot/labs/lab09/leetcode/no0235/Solution01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cn.iocoder.springboot.labs.lab09.leetcode.no0235; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 寻找路径,然后匹配 | ||
*/ | ||
public class Solution01 { | ||
|
||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
// p 节点的路径 | ||
List<TreeNode> pNodes = new ArrayList<>(); | ||
binarySearch(root, p, pNodes); | ||
// q 节点的路径 | ||
List<TreeNode> qNodes = new ArrayList<>(); | ||
binarySearch(root, q, qNodes); | ||
|
||
// 倒序,对比 | ||
for (int i = pNodes.size() - 1; i >= 0; i--) { | ||
TreeNode node = pNodes.get(i); | ||
for (int j = qNodes.size() - 1; j >= 0; j--) { | ||
TreeNode node2 = qNodes.get(j); | ||
if (node.val == node2.val) { | ||
return node; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private void binarySearch(TreeNode root, TreeNode target, List<TreeNode> nodes) { | ||
if (root == null) { // 理论不存在,防御性 | ||
return; | ||
} | ||
|
||
// 添加到路径 | ||
nodes.add(root); | ||
|
||
if (root.val == target.val) { | ||
return; | ||
} | ||
if (root.val > target.val) { | ||
binarySearch(root.left, target, nodes); | ||
} else { | ||
binarySearch(root.right, target, nodes); | ||
} | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
lab-09/src/main/java/cn/iocoder/springboot/labs/lab09/leetcode/no0235/Solution02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cn.iocoder.springboot.labs.lab09.leetcode.no0235; | ||
|
||
/** | ||
* 递归求解父节点 | ||
*/ | ||
public class Solution02 { | ||
|
||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
if (p.val < root.val | ||
&& root.val > q.val) { // 因为 root.val 大于 q.val ,说明 root 的值过大,需要往左走 | ||
return lowestCommonAncestor(root.left, p, q); | ||
} else if (p.val > root.val // 因为 root.val 小于 p.val ,说明 root 的值过小,需要往右走 | ||
&& root.val < q.val) { | ||
return lowestCommonAncestor(root.right, p, q); | ||
} else { // 其他情况,root 都是符合条件的,例如说 root 的泛微在 p and q 之间(或者 q and p 之间)。 | ||
// 为什么直接返回 root 就可以,因为如果不是最接近的父节点,p 和 q 要么在其左边,要么在其右边。 | ||
return root; | ||
} | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
lab-09/src/main/java/cn/iocoder/springboot/labs/lab09/leetcode/no0235/Solution03.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cn.iocoder.springboot.labs.lab09.leetcode.no0235; | ||
|
||
/** | ||
* {@link Solution02} 的改进,非递归方式。 | ||
*/ | ||
public class Solution03 { | ||
|
||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
while (true) { | ||
if (p.val < root.val | ||
&& root.val > q.val) { // 因为 root.val 大于 q.val ,说明 root 的值过大,需要往左走 | ||
root = root.left; | ||
} else if (p.val > root.val // 因为 root.val 小于 p.val ,说明 root 的值过小,需要往右走 | ||
&& root.val < q.val) { | ||
root = root.right; | ||
} else { // 其他情况,root 都是符合条件的,例如说 root 的泛微在 p and q 之间(或者 q and p 之间)。 | ||
return root; | ||
} | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
lab-09/src/main/java/cn/iocoder/springboot/labs/lab09/leetcode/no0235/TreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cn.iocoder.springboot.labs.lab09.leetcode.no0235; | ||
|
||
public class TreeNode { | ||
|
||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
|
||
TreeNode(int x) { val = x; } | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
lab-09/src/main/java/cn/iocoder/springboot/labs/lab09/leetcode/no0236/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cn.iocoder.springboot.labs.lab09.leetcode.no0236; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
if (false) { | ||
TreeNode node3 = new TreeNode(3); | ||
TreeNode node5 = new TreeNode(5); | ||
TreeNode node6 = new TreeNode(6); | ||
TreeNode node7 = new TreeNode(7); | ||
TreeNode node4 = new TreeNode(4); | ||
TreeNode node2 = new TreeNode(2); | ||
TreeNode node1 = new TreeNode(1); | ||
node3.left = node5; | ||
node3.right = node1; | ||
node5.left = node6; | ||
node5.right = node2; | ||
node6.left = node7; | ||
node6.right = node4; | ||
TreeNode result = new Solution01().lowestCommonAncestor(node3, node3, node5); | ||
System.out.println(result.val); | ||
} | ||
if (true) { | ||
TreeNode node3 = new TreeNode(3); | ||
TreeNode node5 = new TreeNode(5); | ||
TreeNode node1 = new TreeNode(1); | ||
TreeNode node6 = new TreeNode(6); | ||
TreeNode node2 = new TreeNode(2); | ||
TreeNode node0 = new TreeNode(0); | ||
TreeNode node8 = new TreeNode(8); | ||
TreeNode node7 = new TreeNode(7); | ||
TreeNode node4 = new TreeNode(4); | ||
node3.left = node5; | ||
node3.right = node1; | ||
node5.left = node6; | ||
node5.right = node2; | ||
node1.left = node0; | ||
node1.right = node8; | ||
node2.left = node7; | ||
node2.right = node4; | ||
TreeNode result = new Solution01().lowestCommonAncestor(node3, node5, node4); | ||
System.out.println(result.val); | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
lab-09/src/main/java/cn/iocoder/springboot/labs/lab09/leetcode/no0236/Solution01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cn.iocoder.springboot.labs.lab09.leetcode.no0236; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class Solution01 { | ||
|
||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
// p 节点的路径 | ||
List<TreeNode> pNodes = new ArrayList<>(); | ||
search(root, p, pNodes, new AtomicBoolean()); | ||
// q 节点的路径 | ||
List<TreeNode> qNodes = new ArrayList<>(); | ||
search(root, q, qNodes, new AtomicBoolean()); | ||
|
||
// 倒序,对比 | ||
for (int i = 0; i < pNodes.size(); i++) { | ||
TreeNode node = pNodes.get(i); | ||
for (int j = 0; j < qNodes.size(); j++) { | ||
TreeNode node2 = qNodes.get(j); | ||
if (node.val == node2.val) { | ||
return node; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private void search(TreeNode root, TreeNode target, List<TreeNode> nodes, AtomicBoolean found) { | ||
if (root == null) { // 理论不存在,防御性 | ||
return; | ||
} | ||
|
||
// 如果当前节点,就是要找的,就添加到 nodes 中 | ||
if (root.val == target.val) { | ||
found.set(true); | ||
nodes.add(root); | ||
return; | ||
} | ||
|
||
// 如果不是,递归子节点 | ||
search(root.left, target, nodes, found); | ||
if (!found.get()) { | ||
search(root.right, target, nodes, found); | ||
} | ||
|
||
// 如果子节点找到,则添加到 nodes 中 | ||
if (found.get()) { | ||
nodes.add(root); | ||
} | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
lab-09/src/main/java/cn/iocoder/springboot/labs/lab09/leetcode/no0236/Solution02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cn.iocoder.springboot.labs.lab09.leetcode.no0236; | ||
|
||
public class Solution02 { | ||
|
||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
// 如果递归,当前节点 root 就是 p 或者 q ,则直接返回 | ||
if (root == null || root == p || root == q) { | ||
return root; | ||
} | ||
|
||
// 遍历左子树 | ||
TreeNode left = lowestCommonAncestor(root.left, p, q); | ||
// 遍历右子节点 | ||
TreeNode right = lowestCommonAncestor(root.right, p, q); | ||
// 上述,因为是先递归,后判断,所以一定会先招到最接近的。然后,就不断向上,返回最接近的了。 | ||
|
||
// 判断父节点 | ||
if (left == null) { // 左子树没找到,那就选择右子树。 | ||
return right; | ||
} | ||
if (right == null) { // 右子树没找到,那就选择左子树。 | ||
return left; | ||
} | ||
return root; // 如果左右子树都找到,说明 root 是它们的父节点 | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
lab-09/src/main/java/cn/iocoder/springboot/labs/lab09/leetcode/no0236/TreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cn.iocoder.springboot.labs.lab09.leetcode.no0236; | ||
|
||
public class TreeNode { | ||
|
||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
|
||
TreeNode(int x) { val = x; } | ||
|
||
} |