//二叉树定义
class TreeNode {
int val;
TreeNode left = null;
TreeNode right = null;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
}
public class FindAndInsertTree01 {
TreeNode root = null;
//插入
public void insert(TreeNode node) {
if(root==null) {
root=node;
}else {
TreeNode current = root;
TreeNode parent = current;
while(true) {
parent=current;
if(current.valvalue){
current=current.left;
}else {
return current;
}
if(current==null)return null;
}
return null;
}
}
public int NumberOf1(int n) {
int count=0;
if(n<0) {
count++;
n=n&0x7FFFFFFF;
}
while(n!=0) {
if(n%2!=0)count++;
n=n/2;
}
return count;
}
//非递归
public int binSearch(int arr[],int k) {
int low=0;
int high=arr.length-1;
while(lowk)high=middle-1;
else low=middle+1;
}
return -1;
}
//递归
public int binSearch(int arr[],int k,int low,int high) {
if(lowk)binSearch(arr,k,low,middle-1);
else binSearch(arr,k,middle+1,high);
}
return -1;
}
public int[] quickSort(int arr[],int low,int high) {
if(low=temp) {
high--;
}
arr[low]=arr[high];
while(low
public ListNode ReverseList(ListNode head) {
ListNode next=null;
ListNode pre=null;
while(head!=null){
next=head.next;
head.next=pre;
pre=head;
head=next;
}
return pre;
}
//递归
public List postorder(TreeNode root) {
List result=new ArrayList<>();
postorder(root,result);
return result;
}
private void postorder(TreeNode root, List result) {
if(root==null)return;
result.add(root.val);
postorder(root.left,result);
postorder(root.right,result);
}
//非递归
public List postorderF(TreeNode root) {
List result=new ArrayList<>();
Stack s=new Stack<>();
Stack temp=new Stack<>();
TreeNode cur=root;
while(!s.isEmpty()) {
cur=s.pop();
temp.add(cur);
if(cur.left!=null)s.push(cur.left);
if(cur.right!=null)s.push(cur.right);
}
while (!temp.isEmpty()) {
cur = temp.pop();
result.add(cur.val);
}
return result;
}
//递归
public List inorder(TreeNode root) {
List result=new ArrayList<>();
inorder(root,result);
return result;
}
private void inorder(TreeNode root, List result) {
if(root==null)return;
inorder(root.left,result);
result.add(root.val);
inorder(root.right,result);
}
//非递归
public List inorderF(TreeNode root) {
List result=new ArrayList<>();
Stack s=new Stack<>();
s.push(root);
TreeNode cur=root;
while(cur != null ||!s.isEmpty()) {
while(cur!=null) {
s.push(cur.left);//添加根节点
cur=cur.left;//添加左子节点
}
cur=s.pop();// 当前栈顶已经是最底层的左节点了,取出栈顶元素,访问该节点
result.add(cur.val);
cur=cur.right;// 添加右节点
}
return result;
}
//递归
public List postorder(TreeNode root) {
List result=new ArrayList<>();
postorder(root,result);
return result;
}
private void postorder(TreeNode root, List result) {
if(root==null)return;
postorder(root.left,result);
postorder(root.right,result);
result.add(root.val);
}
//非递归
public List postorderF(TreeNode root) {
List result=new ArrayList<>();
Stack s=new Stack<>();
Stack temp=new Stack<>();
TreeNode cur=root;
while(!s.isEmpty()) {
cur=s.pop();
temp.add(cur);
if(cur.left!=null)s.push(cur.left);
if(cur.right!=null)s.push(cur.right);
}
while (!temp.isEmpty()) {
cur = temp.pop();
result.add(cur.val);
}
return result;
}
二叉树的遍历 https://segmentfault.com/a/1190000016674584#articleHeader10
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode node=null;
ListNode head=node;
if(list1==null)return list2;
if(list2==null)return list1;
while(list1!=null&&list2!=null) {
if(list1.val<=list2.val) {
if(head==null) {
head=node=list1;
}else {
node.next=list1;
node=node.next;
}
list1=list1.next;
}else {
if(head==null) {
head=node=list2;
}else {
node.next=list2;
node=node.next;
}
list2=list2.next;
}
}
if(list1==null) {
node.next=list2;
}else if(list2==null){
node.next=list1;
}
return head;
}
public int[] twoSum(int[] nums, int target) {
Map map=new HashMap<>();
for(int i=0;i
public ArrayList GetLeastNumbers_Solution(int [] input, int k) {
ArrayList list=new ArrayList<>();
if(input.length=0;i--) {
sink(arr,i,N);
}
while(N>0) {
swap(arr,0,N--);
sink(arr,0,N);
}
}
private static void sink(int[] arr, int i, int N) {
while(2*i+1<=N) {
int k=i*2+1;//左子树
if(karr[k])break;
swap(arr,i,k);
i=k;
}
}
private static void swap(int[] arr, int i, int k) {
int temp=arr[i];
arr[i]=arr[k];
arr[k]=temp;
}