- 树的遍历
- 先序遍历
递归版本
public static void preorderTraversal(List<Integer> list,TreeNode root){ if(root==null) return; list.add(root.value); preorderTraversal1(list,root.left); preorderTraversal1(list,root.right); }
非递归版本
public static List<Integer> preorderTraversal(TreeNode root){ Stack<TreeNode> stack=new Stack<>(); List<Integer> list=new LinkedList<>(); if(root!=null) stack.push(root); while(!stack.isEmpty()){ TreeNode cur= stack.pop(); list.add(cur.value); if(cur.right!=null) stack.push(cur.right); if(cur.left!=null) stack.push(cur.left); } return list; }
- 中序遍历
递归版
非递归版public static void inorderTraversal(List<Integer> list,TreeNode root){ if(root==null) return; inorderTraversal1(list,root.left); list.add(root.value); inorderTraversal1(list,root.right); }
public static List<Integer> inorderTraversal(TreeNode root){ Stack<TreeNode> stack=new Stack<>(); List<Integer> list=new LinkedList<>(); while(root!=null||!stack.isEmpty()){ if(root!=null){ stack.push(root); root=root.left; }else { root=stack.pop(); list.add(root.value); root=root.right; } } return list; }
- 后序遍历
递归版本
非递归版本public static void postorderTraversal(List<Integer> list,TreeNode root){ if(root==null) return; postorderTraversal1(list,root.left); postorderTraversal1(list,root.right); list.add(root.value); }
public static List<Integer> postorderTraversal(TreeNode root) { Stack<TreeNode> stk = new Stack<TreeNode>(); if(root != null) stk.push(root); LinkedList<Integer> res = new LinkedList<Integer>(); while(!stk.isEmpty()){ TreeNode curr = stk.pop(); // 先添加左后添加右,就是先访问右后访问左 if(curr.left != null) stk.push(curr.left); if(curr.right != null) stk.push(curr.right); // 反向添加结果,每次加到最前面 res.offerFirst(curr.value); } return res; }
- 层序遍历
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); if(root==null){ return list; } Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.offer(root); while (!queue.isEmpty()) { TreeNode treeNode = queue.poll(); if (treeNode.left != null) { queue.offer(treeNode.left); } if (treeNode.right != null) { queue.offer(treeNode.right); } list.add(treeNode.val); } return list; }
- 排序算法
- 快速排序
public static void quickSort(int[] n,int left,int right){ int dp; if(left<right){ dp=partition(n,left,right); quickSort(n,left,dp-1); quickSort(n,dp+1,right); System.out.println(Arrays.toString(n)); } } public static int partition(int[] n,int left,int right){ int pivot=n[left]; while(left<right){ while(left<right&&n[right]>pivot) right--; if(left<right) n[left++]=n[right]; while (left < right && n[left] <= pivot) left++; if (left < right) n[right--] = n[left]; } n[left]=pivot; return left; }
- 归并排序
public static void merge(int[] array,int low,int mid,int high){ int[] temp=new int[high-low+1]; int i=low; int j=mid+1; int k=0; while(i<=mid&&j<=high){ if(array[i]<array[j]){ temp[k++]=array[i++]; }else { temp[k++]=array[j++]; } } while (i <= mid) { temp[k++] = array[i++]; } while (j <= high) { temp[k++] = array[j++]; } for (int k2 = 0; k2 < temp.length; k2++) { array[k2 + low] = temp[k2]; } } public static void mergeSort(int[] array,int low,int high){ int mid=low+((high-low)>>1); if(low<high){ mergeSort(array,low,mid); mergeSort(array,mid+1,high); merge(array,low,mid,high); System.out.println(Arrays.toString(array)); } }
- 选择排序
public static void selectSort(int[] array){ for(int i=0;i<array.length;i++){ int k=i; for(int j=i+1;j<array.length;j++){ if(array[j]<array[k]){ k=j; } } if(i!=k){ int temp=array[i]; array[i]=array[k]; array[k]=temp; } } }
- 冒泡排序
public static void bubbleSort(int[] array){ for(int i=0;i<array.length;i++){ for(int j=0;j<array.length-i-1;j++){ if(array[j]>array[j+1]){ int temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } }
- 插入排序
public static void insertSort(int[] array){ for(int i=1;i<array.length;i++){ int target=array[i]; int j=i; while (j>0&&target<array[j-1]){ array[j]=array[j-1]; j--; } array[j]=target; } }
- 堆排序(参考:https://blog.csdn.net/zdp072/article/details/44227317)
private static void heapSort(int[] arr) { // 将待排序的序列构建成一个大顶堆 for (int i = arr.length / 2; i >= 0; i--){ heapAdjust(arr, i, arr.length); } // 逐步将每个最大值的根节点与末尾元素交换,并且再调整二叉树,使其成为大顶堆 for (int i = arr.length - 1; i > 0; i--) { swap(arr, 0, i); // 将堆顶记录和当前未经排序子序列的最后一个记录交换 heapAdjust(arr, 0, i); // 交换之后,需要重新检查堆是否符合大顶堆,不符合则要调整 } } /** * 构建堆的过程 * @param arr 需要排序的数组 * @param i 需要构建堆的根节点的序号 * @param n 下标n之前的元素需要调整(原作者说是数组长度,有歧义) */ private static void heapAdjust(int[] arr, int i, int n) { int child; int father; for (father = arr[i]; leftChild(i) < n; i = child) { child = leftChild(i); // 如果左子树小于右子树,则需要比较右子树和父节点 if (child != n - 1 && arr[child] < arr[child + 1]) { child++; // 序号增1,指向右子树 } // 如果父节点小于孩子结点,则需要交换 if (father < arr[child]) { arr[i] = arr[child]; } else { break; // 大顶堆结构未被破坏,不需要调整 } } arr[i] = father; } // 获取到左孩子结点 private static int leftChild(int i) { return 2 * i + 1; } // 交换元素位置 private static void swap(int[] arr, int index1, int index2) { int tmp = arr[index1]; arr[index1] = arr[index2]; arr[index2] = tmp; }