节点定义
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
前序/中序/后序遍历
//**********递归的先序遍历**********
public static void recursivePreOrder(TreeNode p) {
if (p == null) return;
visit(p);
recursivePreOrder(p.left);
recursivePreOrder(p.right);
}
//**********递归的中序遍历**********
public static void recursiveInOrder(TreeNode p) {
if (p == null) return;
recursiveInOrder(p.left);
visit(p);
recursiveInOrder(p.right);
}
//**********递归的后序遍历**********
public static void recursivePostOrder(TreeNode p) {
if (p == null) return;
recursivePostOrder(p.left);
recursivePostOrder(p.right);
visit(p);
}
层序遍历
public int maxDepth(Node root) { //层序遍历解法
if(root == null)
return 0;
if(root.children.size() == 0)
return 1;
int depth = 0;
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while(!queue.isEmpty()){
int count = queue.size();
depth++;
while(count > 0){
Node node = queue.poll();
if(node.children.size() != 0)
queue.addAll(node.children);
count--;
}
}
return depth;
}
深度遍历
DFS递归写法
public int maxDepth(Node root) {
if(root == null)
return 0;
int depth = 0;
for(int i = 0;i<root.children.size();i++){
depth = Math.max(depth,maxDepth(root.children.get(i)));
}
return depth+1;
}
快速排序
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
fastSort(input, 0, input.length - 1);
ArrayList<Integer> list = new ArrayList(k);
for(int i = 0; i < k; i++){
list.add(input[i]);
}
return list;
}
public static void fastSort(int[] array,int start,int tail) {
if(start >= tail) {
return;
}
//将第一个元素作为比较元素,从第二个开始到最后一个执行快速排序算法
int begin = start;
int end = tail;
int key = array[start];
while(begin < end) {
while(array[end] >= key && begin < end) {
end = end - 1;
}
while(array[begin] <= key && begin < end) {
begin = begin + 1;
}
if(end > begin) {
int temp = array[begin];
array[begin] = array[end];
array[end] = temp;
}
}
array[start] = array[begin];
array[begin] = key;
fastSort(array,start,begin - 1);
fastSort(array,begin + 1,tail);
}
// public void fastSort1(int[] input, int start, int end){
// if(start >= end) {
// return;
// }
// int key = input[start];
// int head = start;
// int tail = end;
// while(head < tail){
// while(head < tail && input[tail] >= key){
// tail--;
// }
// swap(input, head, tail); // 费解,为什么fastSort1 必须要执行这行代码才行
// while(head < tail && key >= input[head]){
// head++;
// }
// swap(input, head, tail);
// }
// fastSort(input, start, head - 1);
// fastSort(input, head + 1, end);
// }
public void swap(int[] input, int one, int two){
int temp = input[one];
input[one] = input[two];
input[two] = temp;
}
}
归并排序
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
mergeSort(input, 0, input.length-1);
k = input.length;
ArrayList<Integer> list = new ArrayList(k);
for(int i = 0; i < k; i++){
list.add(input[i]);
}
return list;
}
public void mergeSort(int[] input, int left, int right){
int mid = (left + right)/2;
if(left < mid){
mergeSort(input, left, mid);
mergeSort(input, mid + 1, right);
merge(input, left, mid, right);
}
}
/**
* 合并两个有序数组
**/
public void merge(int[] input, int l, int mid, int r){
int lIndex = l;
int rIndex = mid + 1;
int[] item = new int[r - l + 1];
int itemIndex = 0;
while(lIndex <= mid && rIndex <= r){
if(input[lIndex] <= input[rIndex]){
item[itemIndex++] = input[lIndex++];
} else {
item[itemIndex++] = input[rIndex++];
}
}
while(lIndex <= mid){
item[itemIndex++] = input[lIndex++];
}
while(rIndex <= r){
item[itemIndex++] = input[rIndex++];
}
for(int i = 0; i < itemIndex; i++){
input[l + i] = item[i];
}
}
}