bts
This commit is contained in:
parent
fac985ee27
commit
d0f6cb7c3a
66
src/main/java/cn/whaifree/leetCode/Tree/LeetCode235.java
Normal file
66
src/main/java/cn/whaifree/leetCode/Tree/LeetCode235.java
Normal file
@ -0,0 +1,66 @@
|
||||
package cn.whaifree.leetCode.Tree;
|
||||
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/1/30 21:10
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode235 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
TreeNode treeNode = TreeNode.constructTreeByArray(6,2,8,0,4,7,9,null,null,3,5);
|
||||
|
||||
new Solution().lowestCommonAncestor(treeNode, treeNode.left.right, treeNode.left).printTree();
|
||||
}
|
||||
|
||||
class Solution {
|
||||
/**
|
||||
* 1. p,q在两边,直接返回
|
||||
* 2. pq在左边,向左递归
|
||||
* 3. pq在右边,向右递归
|
||||
* @param root
|
||||
* @param p
|
||||
* @param q
|
||||
* @return
|
||||
*/
|
||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
// if (root == null) {
|
||||
// return null;
|
||||
// }
|
||||
// if (root == p || root == q) {
|
||||
// // 找到元素
|
||||
// return root;
|
||||
// }
|
||||
|
||||
// if (p.val < root.val && q.val > root.val) {
|
||||
// return root;
|
||||
// }
|
||||
// if (p.val > root.val && q.val < root.val) {
|
||||
// return root;
|
||||
// }
|
||||
|
||||
// 只有这个有用
|
||||
if (p.val > root.val && q.val > root.val) {
|
||||
// 向右递归
|
||||
return lowestCommonAncestor(root.right, p, q);
|
||||
} else if (p.val < root.val && q.val < root.val) {
|
||||
// 向左递归
|
||||
return lowestCommonAncestor(root.left, p, q);
|
||||
}
|
||||
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
|
||||
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor1(root.left, p, q);
|
||||
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor1(root.right, p, q);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
}
|
100
src/main/java/cn/whaifree/leetCode/Tree/LeetCode236.java
Normal file
100
src/main/java/cn/whaifree/leetCode/Tree/LeetCode236.java
Normal file
@ -0,0 +1,100 @@
|
||||
package cn.whaifree.leetCode.Tree;
|
||||
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/1/30 17:35
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode236 {
|
||||
@Test
|
||||
public void test() {
|
||||
TreeNode treeNode = TreeNode.constructTreeByArray(1, 2, 3, 4, 5, 6);
|
||||
|
||||
new Solution1().lowestCommonAncestor(treeNode, treeNode.left.right, treeNode.left).printTree();
|
||||
}
|
||||
|
||||
|
||||
class Solution {
|
||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
|
||||
// 先找到两个树
|
||||
// 用两个List记录路径
|
||||
// 挨个对比,找到最后一个公共的点
|
||||
ArrayList<TreeNode> e1 = new ArrayList<>();
|
||||
findNode(root, p, e1);
|
||||
ArrayList<TreeNode> e2 = new ArrayList<>();
|
||||
findNode(root, q, e2);
|
||||
|
||||
|
||||
|
||||
|
||||
int minSize = Math.min(e1.size(), e2.size());
|
||||
int index = minSize - 1;
|
||||
while (index >= 0) {
|
||||
if (e1.get(index) == e2.get(index)) {
|
||||
return e1.get(index);
|
||||
}
|
||||
index--;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean findNode(TreeNode root, TreeNode wantFindNode, List<TreeNode> path) {
|
||||
|
||||
if (root==null) return false;
|
||||
path.add(root);
|
||||
if (root == wantFindNode) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean left = findNode(root.left, wantFindNode, path);
|
||||
if (left == true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean right = findNode(root.right, wantFindNode, path);
|
||||
if (right == true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
path.remove(path.size() - 1);
|
||||
|
||||
return left || right;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
if (root==null) return null;
|
||||
// 左右找到两个树
|
||||
// 如果找到树了,返回该节点
|
||||
if (root.val == p.val || root.val == q.val) {
|
||||
return root;
|
||||
}
|
||||
|
||||
TreeNode left = lowestCommonAncestor(root.left, p, q);
|
||||
TreeNode right = lowestCommonAncestor(root.right, p, q);
|
||||
|
||||
if(left == null && right == null) { // 若未找到节点 p 或 q
|
||||
return null;
|
||||
}else if(left == null && right != null) { // 若找到一个节点
|
||||
return right;
|
||||
}else if(left != null && right == null) { // 若找到一个节点
|
||||
return left;
|
||||
}else { // 若找到两个节点
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
91
src/main/java/cn/whaifree/leetCode/Tree/LeetCode501.java
Normal file
91
src/main/java/cn/whaifree/leetCode/Tree/LeetCode501.java
Normal file
@ -0,0 +1,91 @@
|
||||
package cn.whaifree.leetCode.Tree;
|
||||
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/1/30 11:07
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode501 {
|
||||
class Solution {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
int maxFrequency = 0;
|
||||
public int[] findMode(TreeNode root) {
|
||||
circle(root, 0);
|
||||
return null;
|
||||
}
|
||||
public void circle(TreeNode root, int maxFrequency) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.right != null && root.right.val == root.val) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
TreeNode root = TreeNode.constructTreeByArray(6,2,8,0,4,7,9,null,null,2,6);
|
||||
for (int i : new Solution1().findMode(root)) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
class Solution1 {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
// 记录当前出现次数最多的
|
||||
int max = 0;
|
||||
TreeNode pre = null;
|
||||
int sum = 0;
|
||||
|
||||
public int[] findMode(TreeNode root) {
|
||||
circle(root);
|
||||
int size = res.size();
|
||||
int[] r = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
r[i] = res.get(i);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
public void circle(TreeNode root) {
|
||||
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
circle(root.left);
|
||||
|
||||
int rootValue = root.val;
|
||||
// 如果当前节点是root,那么pre就是左节点
|
||||
// 如果当前节点是right,那么pre就是root
|
||||
if (pre == null || rootValue != pre.val) {
|
||||
sum = 1;
|
||||
}else {
|
||||
sum++;
|
||||
}
|
||||
|
||||
// 如果出现次数比当前已经有的最大值都多,那么重新统计
|
||||
if (sum > max) {
|
||||
max = sum;
|
||||
res.clear();
|
||||
res.add(rootValue);
|
||||
} else if (sum == max) {
|
||||
res.add(rootValue);
|
||||
}
|
||||
// 记录上一个节点
|
||||
pre = root;
|
||||
|
||||
|
||||
circle(root.right);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user