目录
1.二叉树的最近公共祖先
2.从前序与中序遍历序列构造二叉树
3.最小k个数
1.二叉树的最近公共祖先
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (root == p || root == q) {
return root;
}
TreeNode leftTree = lowestCommonAncestor(root.left, p, q);
TreeNode rightTree = lowestCommonAncestor(root.right, p, q);
if (leftTree != null && rightTree != null) {
return root;
} else if (leftTree != null) {
return leftTree;
} else {
return rightTree;
}
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
if (root == null) {
return false;
}
stack.push(root);
if (root == node) {
return true;
}
boolean ret1 = getPath(root.left, node, stack);
if (ret1) {
return true;
}
boolean ret2 = getPath(root.right, node, stack);
if (ret2) {
return true;
}
stack.pop();
return false;
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode P, TreeNode q) {
if (root == null) {
return null;
}
Stack<TreeNode> stackp = new Stack<>();
Stack<TreeNode> stackq = new Stack<>();
getPath(root, P, stackp);
getPath(root, q, stackq);
int sizep = stackp.size();
int sizeq = stackq.size();
if (sizep > sizeq) {
int size = sizep - sizeq;
while (size != 0) {
stackp.pop();
size--;
}
} else {
int size = sizeq - sizep;
while (size != 0) {
stackq.pop();
size--;
}
}
while (!stackp.isEmpty() && !stackq.isEmpty()) {
if (stackq.peek() == stackp.peek()) {
return stackq.peek();
} else {
stackq.pop();
stackp.pop();
}
}
return null;
}
}
2.从前序与中序遍历序列构造二叉树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int preindex;
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeChild(preorder,inorder,0,inorder.length - 1);
}
public TreeNode buildTreeChild(int[] preorder,int[] inorder,int inbegin ,int inend){
//这种情况下 表明当前root没有子树了
if(inbegin > inend){
return null;
}
TreeNode root = new TreeNode(preorder[preindex]);
int rootIndex = findVal(inorder,inbegin,inend,preorder[preindex]);
preindex++;
root.left = buildTreeChild(preorder,inorder,inbegin,rootIndex - 1);
root.right = buildTreeChild(preorder,inorder,rootIndex + 1,inend);
return root;
}
private int findVal(int[] inorder ,int inbegin,int inend,int val){
for(int i = inbegin ; i <= inend ; i++){
if(inorder[i] == val){
return i;
}
}
return -1;
}
}
3.最小k个数
class Solution {
//时间复杂度为nlogN + klogN
public int[] smallestK(int[] arr, int k) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
for(int i = 0; i < arr.length;i++){
priorityQueue.offer(arr[i]);
}
int[] ret = new int[k];
for(int i = 0; i < k ; i++){
ret[i] = priorityQueue.poll();
}
return ret;
}
}
方法二
class Intmp implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
}
class Solution {
public int[] smallestK(int[] arr, int k) {
int[] ret = new int[k];
if (arr == null || k == 0) {
return ret;
}
//k*logk
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(k, new Intmp());
for (int i = 0; i < k; i++) {
priorityQueue.offer(arr[i]);
}
//(n - k)logk
for (int i = k; i < arr.length; i++) {
int peekVal = priorityQueue.peek();
if (arr[i] < peekVal) {
priorityQueue.poll();
priorityQueue.offer(arr[i]);
}
}
for (int i = 0; i < k; i++) {
ret[i] = priorityQueue.poll();
}
return ret;
}
}