二叉树系列算法学习(递归&迭代)
- 数组建二叉树
- 深度(前序、中序、后序)遍历
- 广度(层次序)遍历
- 树的深度
建树
1
/ \
2 3
/ \ / \
4 5 6 7
\ / /\ /
8 9 10 11 12
执行结果
代码
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class TreeSeriesOne {
//TreeNode:树节点定义
class TreeNode{
int val;
TreeNode right = null;
TreeNode left = null;
TreeNode(int val){
this.val = val;
}
}
//buildTree: 根据数组建树
public TreeNode buildTree(int[] array, int index){
if(array == null || array.length == 0 || index < 0 || index >= array.length || array[index] == -1) return null;
TreeNode root = new TreeNode(array[index]);
root.left = buildTree(array, 2*index+1);
root.right = buildTree(array, 2*index+2);
return root;
}
//preOrder:迭代前序遍历
public ArrayList<Integer> preOrder(TreeNode root){
ArrayList<Integer> list = new ArrayList<Integer>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
while (root!=null || !stack.isEmpty()){
while (root!=null){
list.add(root.val);
stack.push(root);
root = root.left;
}
if(!stack.isEmpty()){
root = stack.pop().right;
}
}
return list;
}
//midOrder:迭代中序遍历
public ArrayList<Integer> midOrder(TreeNode root){
ArrayList<Integer> list = new ArrayList<Integer>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
while (root!=null || !stack.isEmpty()){
while(root!=null){
stack.push(root);
root = root.left;
}
if(!stack.isEmpty()){
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
return list;
}
//lastOrder:迭代后序遍历
public ArrayList<Integer> lastOrder(TreeNode root){
ArrayList<Integer> list = new ArrayList<Integer>();
if(root == null) return list;
Stack<TreeNode> stack1 = new Stack<TreeNode>();
Stack<TreeNode> stack2 = new Stack<TreeNode>();
while (root!=null || !stack1.isEmpty()){
while(root!=null){
stack1.push(root);
stack2.push(root);
root = root.right;
}
if(!stack1.isEmpty()){
root = stack1.pop().left;
}
}
while (!stack2.isEmpty())
list.add(stack2.pop().val);
return list;
}
//preOder1:递归前序遍历
public void preOrder1(ArrayList<Integer> list, TreeNode root){
if(root==null) return;
list.add(root.val);
preOrder1(list, root.left);
preOrder1(list, root.right);
}
//midOrder1:递归中序遍历
public void midOrder1(ArrayList<Integer> list, TreeNode root){
if(root==null) return ;
midOrder1(list, root.left);
list.add(root.val);
midOrder1(list, root.right);
}
//lastOrder1:递归后序遍历
public void lastOrder1(ArrayList<Integer> list, TreeNode root){
if(root==null) return;
lastOrder1(list, root.left);
lastOrder1(list, root.right);
list.add(root.val);
}
//levelOrder:层次序遍历,一次性打印
public ArrayList<Integer> levelOrder(TreeNode root){
ArrayList<Integer> list = new ArrayList<Integer>();
if(root == null) return list;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
root = queue.poll();
list.add(root.val);
if(root.left != null){
queue.offer(root.left);
}
if(root.right != null){
queue.offer(root.right);
}
}
return list;
}
//levelOrder1:层次序遍历,分行打印
public ArrayList<ArrayList<Integer> > levelOrder1(TreeNode root){
ArrayList<ArrayList<Integer> > list = new ArrayList<ArrayList<Integer> >();
ArrayList<Integer> sublist = new ArrayList<Integer>();
if(root == null) return list;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int tobePrintNum = 1;
int nextLevelNum = 0;
while(!queue.isEmpty()){
root = queue.poll();
sublist.add(root.val);
if(root.left != null){
queue.offer(root.left);
nextLevelNum++;
}
if(root.right != null){
queue.offer(root.right);
nextLevelNum++;
}
tobePrintNum--;
if(tobePrintNum == 0){
list.add(new ArrayList<>(sublist));
sublist.clear();
tobePrintNum = nextLevelNum;
nextLevelNum = 0;
}
}
return list;
}
//levelOrder2:层次序遍历,之字形分行打印
public ArrayList<ArrayList<Integer> > levelOrder2(TreeNode root){
ArrayList<ArrayList<Integer> > list = new ArrayList<ArrayList<Integer> >();
ArrayList<Integer> sublist = new ArrayList<Integer>();
if(root == null) return list;
Stack<TreeNode> evenStack = new Stack<TreeNode>();
Stack<TreeNode> oddStack = new Stack<TreeNode>();
oddStack.push(root);
boolean isOddLevel = true;
while(!evenStack.isEmpty() || !oddStack.isEmpty()){
if(isOddLevel){
root = oddStack.pop();
sublist.add(root.val);
if(root.left != null) evenStack.push(root.left);
if(root.right != null) evenStack.push(root.right);
if(oddStack.isEmpty()){
isOddLevel = false;
list.add(new ArrayList<Integer>(sublist));
sublist.clear();
}
}
else{
root = evenStack.pop();
sublist.add(root.val);
if(root.right != null) oddStack.push(root.right);
if(root.left != null) oddStack.push(root.left);
if(evenStack.isEmpty()){
isOddLevel = true;
list.add(new ArrayList<Integer>(sublist));
sublist.clear();
}
}
}
return list;
}
//treeDepth:递归求树的深度
public int treeDepth(TreeNode root){
if(root == null) return 0;
int leftDepth = treeDepth(root.left);
int rightDepth = treeDepth(root.right);
return leftDepth < rightDepth ? rightDepth+1 : leftDepth+1;
}
//treeDepth1:层次序遍历求树的深度
public int treeDepth1(TreeNode root){
if(root == null) return 0;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int depth = 0;
while(!queue.isEmpty()){
int num = queue.size();
depth++;
while(num != 0){
num--;
root = queue.poll();
if(root.left != null) queue.offer(root.left);
if(root.right != null) queue.offer(root.right);
}
}
return depth;
}
public static void main(String[] args) {
TreeSeriesOne s = new TreeSeriesOne();
//测试一:buildTree
// 1
// 2 3
// 4 5 6 7
//-1 8 9 -1 10 11 12 -1
int[] array = {1,2,3,4,5,6,7,-1,8,9,-1,10,11,12,-1};
TreeNode root = s.buildTree(array, 0);
//测试二:前序遍历 [1,2,4,8,5,9,3,6,10,11,7,12]
ArrayList<Integer> preList = s.preOrder(root);
System.out.println("迭代前序遍历:"+preList.toString());
//测试三:中序遍历 [4,8,2,9,5,1,10,6,11,3,12,7]
ArrayList<Integer> midList = s.midOrder(root);
System.out.println("迭代中序遍历:"+midList.toString());
//测试四:后序遍历 [8,4,9,5,2,10,11,6,12,7,3,1]
ArrayList<Integer> lastList = s.lastOrder(root);
System.out.println("迭代后序遍历:"+lastList.toString());
//测试五:前序遍历 [1,2,4,8,5,9,3,6,10,11,7,12]
ArrayList<Integer> preList1 = new ArrayList<Integer>();
s.preOrder1(preList1, root);
System.out.println("递归前序遍历:"+preList1.toString());
//测试六:中序遍历 [4,8,2,9,5,1,10,6,11,3,12,7]
ArrayList<Integer> midList1 = new ArrayList<Integer>();
s.midOrder1(midList1, root);
System.out.println("递归中序遍历:"+midList1.toString());
//测试七:后序遍历 [8,4,9,5,2,10,11,6,12,7,3,1]
ArrayList<Integer> lastList1 = new ArrayList<Integer>();
s.lastOrder1(lastList1, root);
System.out.println("递归后序遍历:"+lastList1.toString());
//测试八:层次序遍历,一次性打印 [1,2,3,4,5,6,7,8,9,10,11,12]
ArrayList<Integer> levelList = s.levelOrder(root);
System.out.println("层次序遍历:"+levelList.toString());
//测试九:层次序遍历,分行打印 [[1],[2,3],[4,5,6,7],[8,9,10,11,12]]
ArrayList<ArrayList<Integer> > levelList1 = s.levelOrder1(root);
System.out.println("层次序遍历:"+levelList1.toString());
//测试十:层次序遍历,分行之字形打印 [[1],[2,3],[4,5,6,7],[8,9,10,11,12]]
ArrayList<ArrayList<Integer> > levelList2 = s.levelOrder2(root);
System.out.println("层次序遍历之字形:"+levelList2.toString());
//测试十一:递归求树的深度
System.out.println("递归求树的深度:"+s.treeDepth(root));
//测试十二:层次序遍历求树的深度
System.out.println("层次序遍历求树的深度:"+s.treeDepth1(root));
}
}