遍历、求最大深度、最大宽度的代码
package binaryTreeTest;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Traversal {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
TreeNode node6 = new TreeNode(6);
TreeNode node7 = new TreeNode(7);
TreeNode node0 = new TreeNode(0);
node0.left = node1;
node0.right = node2;
node1.left = node3;
node1.right = node4;
node2.left = node5;
node2.right = node6;
node6.left = node7;
//preorder1(node0);
System.out.println(width(node0));
}
//先序遍历递归版
public static void preorder(TreeNode head){
if(head == null)
return;
System.out.println(head.val);
preorder(head.left);
preorder(head.right);
}
//先序遍历非递归版
public static void preorder1(TreeNode head){
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(head);
System.out.println(head.val);
while(!stack.isEmpty()){
while(head.left != null){
System.out.println(head.left.val);
stack.push(head.left);
head = head.left;
}
head = stack.pop();
while(head.right == null){
if(stack.isEmpty())
return;
head = stack.pop();
}
System.out.println(head.right.val);
stack.push(head.right);
head = head.right;
}
}
//中序遍历
public static void inorder(TreeNode head){
if(head == null)
return;
inorder(head.left);
System.out.println(head.val);
inorder(head.right);
}
//后序遍历
public static void postorder(TreeNode head){
if(head == null)
return;
postorder(head.left);
postorder(head.right);
System.out.println(head.val);
}
//层次遍历
public static void levelorder(TreeNode head){
Queue<TreeNode> queue = new LinkedBlockingQueue<TreeNode>();
queue.offer(head);
while(!queue.isEmpty()){
System.out.println(queue.peek().val);
if(queue.peek().left != null){
queue.offer(queue.peek().left);
}
if(queue.peek().right != null){
queue.offer(queue.peek().right);
}
queue.remove();
}
}
//求树的最大深度
public static int depth(TreeNode head){
Queue<TreeNode> queue = new LinkedBlockingQueue<TreeNode>();
int cur = 0;
int last = 0;
int level = 0;
queue.offer(head);
TreeNode tmp = new TreeNode(-1);
while(!queue.isEmpty()){
last = queue.size();
cur = 0;
while(cur < last){
tmp = queue.poll();
cur++;
if(tmp.left != null){
queue.offer(tmp.left);
}
if(tmp.right != null){
queue.offer(tmp.right);
}
}
level++;
}
return level;
}
//求树的最大宽度
public static int width(TreeNode head){
Queue<TreeNode> queue = new LinkedBlockingQueue<TreeNode>();
int cur = 0;
int last = 0;
int maxWidth = 0;
queue.offer(head);
TreeNode tmp = new TreeNode(-1);
while(!queue.isEmpty()){
last = queue.size();
maxWidth = Math.max(maxWidth, last);
cur = 0;
while(cur < last){
tmp = queue.poll();
cur++;
if(tmp.left != null){
queue.offer(tmp.left);
}
if(tmp.right != null){
queue.offer(tmp.right);
}
}
}
return maxWidth;
}
}
二叉树类
package binaryTreeTest;
public class TreeNode {
int val;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val){
this.val = val;
}
}