1,二叉树的非递归前序遍历
public class BinNode<T> {
private T data;
private BinNode<T> leftChild;
private BinNode<T> rightChild;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BinNode<T> getLeftChild() {
return leftChild;
}
public void setLeftChild(BinNode<T> leftChild) {
this.leftChild = leftChild;
}
public BinNode<T> getRightChild() {
return rightChild;
}
public void setRightChild(BinNode<T> rightChild) {
this.rightChild = rightChild;
}
}
public static void preOrder(BinNode<String> root) {
Stack<BinNode<String>> s = new Stack<BinNode<String>>();
while(root!= null ||!s.empty()) {
while(root!=null) {
visit(root);
s.push(root);
root = root.getLeftChild();
}
if(!s.empty()) {
root=s.pop();
root=root.getRightChild();
}
}
}
2,把二元查找树转变成排序的双向链表
解决思路,把二叉查找树进行中序遍历,然后增加子节点指向父节点的指针。
3,求二叉树的最大距离
4,二叉树按层遍历
public void printBinTree(BinTreeNode root) {
if(null == root)
return;
Queue<BinTreeNode> queue = new LinkedList<BinTreeNode>();
queue.offer(root);
while(queue.size() > 0) {
BinTreeNode node = queue.poll();
visit(node);
if(node.left != null) {
queue.offer(node.left);
}
if(node.right != null) {
queue.offer(node.right);
}
}
}
5,把字符串转换成整数
/**
* 把字符串转换成整数
*/
public void parseInt(String str) {
if (str == null || str.equals(""))
return;
int temp = '1' - 1;
int sum = 0;
int lowrange = '0' - temp;
int highrange = '9' - temp;
int j = 1; //控制乘数
for (int i = str.length() - 1; i >= 0; i--) {
char c = str.charAt(i);
int number = c - temp;
if (number >= lowrange && number <= highrange) {
if (i != str.length() - 1) {
j = j * 10;
sum = sum + number * j;
} else {
sum = sum + number;
}
}
}
if ('-' == str.charAt(0)) {
sum = 0 - sum;
}
System.out.println(sum);
}
6,求一个整数的二进制中1的个数
<pre name="code" class="java">package com.dong.istudy.btree;
public class BinaryAlg {
public static void main(String[] args) {
System.out.println(sumNumberOf2(129));
}
/**
* 在了解位移之前,先了解一下正数和负数的二进制表示形式以及关系:
举例15和-15:
15 的原码: 00000000 00000000 00000000 00001111
补码: 11111111 11111111 11111111 11110000
+1 =
-15的原码:11111111 11111111 11111111 11110001
负数的原码即为:正数的原码取反,再加1。
该程序只能向左移动flag,而不能向右移动number,因为number可能为负数造成死循环
*/
public static int sumNumberOf2(int number) {
int count = 0;
int flag = 1;
while(flag <= number) { //flag为number的二倍的时候停止
/**
* &与运算,如:
* int a=129;
int b=128;
System.out.println("a 和b 与的结果是:"+(a&b));
“a”的值是129,转换成二进制就是10000001,而“b”的值是128,转换成二进制就是10000000。
根据与运算符的运算规律,只有两个位都是1,结果才是1,可以知道结果就是10000000,即128。
*/
if((number & flag) == flag)
count ++;
flag = flag << 1;
}
return count;
}
}
7,十进制转8进制,16进制
例如8进制来说,对十进制进行除以8,把余数放入到栈,然后把除后的整数商再进行除以8,一直到商为0,然后依次出栈就是了
public static void main(String[] args) {
test(233, 16);
}
public static void test(int number, int jinzhi) {
char[] array = new char[15];
//如果是16进制的话,把大于9的余数进行替换,代码就不写了
array[10] = 'A';
array[11] = 'B';
array[12] = 'C';
array[13] = 'D';
array[14] = 'E';
array[15] = 'F';
java.util.Stack<Integer> stack = new java.util.Stack<Integer>();
while (number > 0) {
int i = number % jinzhi;
stack.push(i);
number = number / jinzhi;
}
while (!stack.isEmpty()) {
System.out.print(stack.pop());
}
}
8,求树的深度
package com.dong.alg.dp;
public class Deep {
public static void main(String[] args) {
}
public static int getDeep(Node root) {
if(root == null) {
return 0;
}
int nleft = getDeep(root.left);
int nright = getDeep(root.right);
return (nleft > nright) ? (nleft + 1) : (nright + 1);
}
}
class Node {
String data;
Node left;
Node right;
}