package Suanfa;
import java.util.*;
class BinaryTreeNode {
int val;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
public class 二叉树遍历 {
public static void main(String[] args) {
BinaryTreeNode root = new BinaryTreeNode(1);
BinaryTreeNode node2= new BinaryTreeNode(2);
BinaryTreeNode node3 = new BinaryTreeNode(3);
BinaryTreeNode node4 = new BinaryTreeNode(4);
BinaryTreeNode node5 = new BinaryTreeNode(5);
root.left = node2;
root.right = node3;
node2.left=node4;
node2.right=node5;
postorderTraversal2(root);
}
// 层次遍历
public static void levelTraverse(BinaryTreeNode tree) {
if (tree == null) {
return;
}
Queue<BinaryTreeNode> queue = new LinkedList<>();
queue.add(tree);
while (!queue.isEmpty()) {
BinaryTreeNode temp = queue.remove();
System.out.print(temp.val + " ");
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
}
}
// 前序遍历(递归实现)
public static void preOrderTraverse1(BinaryTreeNode tree) {
if (tree != null) {
System.out.println(tree.val);
preOrderTraverse1(tree.left);
preOrderTraverse1(tree.right);
}
}
// 前序遍历(非递归实现)
// 访问根节点,根节点入栈
// 左孩子不为空,将左孩子赋值给根
// 左孩子为空,栈顶出栈,访问右孩子
public static void preOrderTraverse2(BinaryTreeNode tree) {
if (tree == null) {
return;
}
Stack<BinaryTreeNode> stack = new Stack<>();
BinaryTreeNode pNode = tree;
while (pNode != null || !stack.isEmpty()) {
if (pNode != null) {
System.out.println(pNode.val);
stack.push(pNode);
pNode = pNode.left;
} else {
BinaryTreeNode node = stack.pop();
pNode = node.right;
}
}
}
// 非递归中序遍历
//左孩子不为空,p入栈,左孩子置为当前P
//左孩子为空,则取栈顶出栈,当前p置为栈顶的右孩子
//直到p为null且栈为空则遍历结束
public static void inorderTraversal(BinaryTreeNode tree) {
Stack<BinaryTreeNode> stack = new Stack<>();
while (tree != null || !stack.isEmpty()) {
while (tree != null) {
stack.add(tree);
tree = tree.left;
}
tree = stack.pop();
System.out.println(tree.val);
tree = tree.right;
}
}
//后序遍历非递归
public static void postorderTraversal(BinaryTreeNode tree){
Stack<BinaryTreeNode>stack1=new Stack<>();
Stack<Integer>stack2=new Stack<>();
int i=1;
while(tree!=null||!stack1.isEmpty()){
while (tree!=null) {
stack1.push(tree);
stack2.push(0);
tree=tree.left;
}
while (!stack1.empty()&&stack2.peek()==i) {
stack2.pop();
System.out.println(stack1.pop().val);
}
if(!stack1.empty()){
stack2.pop();
stack2.push(1);
tree=stack1.peek();
tree=tree.right;
}
}
}
//后序遍历2(左右根)
//颠倒一下是:根右左
//跟根前序遍历类似,把前序遍历的左右颠倒就好了
public static void postorderTraversal2(BinaryTreeNode tree){
List<Integer>list=new ArrayList<>();
if (tree == null) {
return;
}
Stack<BinaryTreeNode> stack = new Stack<>();
BinaryTreeNode pNode = tree;
while (pNode != null || !stack.isEmpty()) {
if (pNode != null) {
//System.out.println(pNode.val);
list.add(pNode.val);
stack.push(pNode);
pNode = pNode.right;
} else {
BinaryTreeNode node = stack.pop();
pNode = node.left;
}
}
Collections.reverse(list);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
最大高度
//二叉树最大深度
public static int depth(BinaryTreeNode root) {
if (root == null)
return 0;
//根节点
if (root.left == null && root.right == null)
return 1;
if (root.left == null)
return depth(root.right) + 1;
if (root.right == null)
return depth(root.left) + 1;
//加上根
return Math.max(depth(root.left), depth(root.right)) + 1;
}