funcpartition(arr []int, left, right int)int { // [left, right] pivotVal := arr[left] // 取第一个数作为枢轴的值 for left < right { for left < right && pivotVal <= arr[right] { right-- } arr[left] = arr[right] for left < right && pivotVal >= arr[left] { left++ } arr[right] = arr[left] } arr[left] = pivotVal return left }
funcquicksort(arr []int, left, right int) { // [left, right] if left < right { pivot := partition(arr, left, right) quicksort(arr, left, pivot-1) quicksort(arr, pivot+1, right) } }
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ funcinorderTraversal(root *TreeNode) []int { ans := make([]int, 0) inorder(root, ans) return ans }
funcinorder(root *TreeNode, ans []int) { if root == nil { return } inorder(root.Left, ans) ans = append(ans, root.Val) inorder(root.Right, ans) }