package tree;
import java.util.*;
public class 树的遍历 {
/**
* 基础的先序,中序,后序
* 从图中引进的层次遍历,蛇形遍历
* 基于先序中序重构造二叉树
*/
//定义树结点
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
//preOrder
public static void preOrderrecur(Node node) {
if (node == null) return;
System.out.println(node.value);
preOrderrecur(node.left);
preOrderrecur(node.right);
}
public static void preOrderStack(Node node) {
if (node != null) {
Stack<Node> stack = new Stack<Node>();
stack.push(node);
while (!stack.isEmpty()) {
Node cur = stack.pop();
System.out.println(cur.value + " ");
if (cur.right != null) {
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(cur.left);
}
}
}
}
//inOrder
public static void inOrderRecur(Node node) {
if (node == null) return;
inOrderRecur(node.left);
System.out.print(node.value + " ");
inOrderRecur(node.right);
}
public static void inOrderStack(Node node) {
if (node != null) {
Stack<Node> stack = new Stack<>();
stack.push(node);
while (!stack.isEmpty() || node != null) {
if (node != null) {
stack.push(node);
node = node.left;
} else {
node = stack.pop();
System.out.println(node.value + " ");
node = node.right;
}
}
}
System.out.println();
}
//postOrder
public static void postOrderrecur(Node node) {
if (node == null) return;
postOrderrecur(node.left);
postOrderrecur(node.right);
System.out.print(node.value + " ");
}
public static void postOrderStack(Node node) {
if (node != null) {
Stack<Node> stack1 = new Stack<>();
Stack<Node> stack2 = new Stack<>();
stack1.push(node);
while (!stack1.isEmpty()) {
Node cur = stack1.pop();
stack2.push(cur);
if (cur.left != null) {
stack1.push(cur.left);
}
if (cur.right != null) {
stack1.push(cur.right);
}
}
while (!stack2.isEmpty()) {
Node cur = stack2.pop();
System.out.print(cur.value + " ");
}
}
}
//层次遍历 就是bfs 用queue
public static void bfs(Node node){
if(node == null) return;
Queue<Node> q = new LinkedList<>();
q.add(node);
while(!q.isEmpty()){
Node cur = q.poll();
System.out.println(cur.value+ " ");
if(cur.left != null){
q.add(cur.left);
}
if(cur.right != null){
q.add(cur.right);
}
}
System.out.println();
}
//蛇形层次遍历 就是层次遍历加一个flag
public static void snakebfs(Node node){
if(node == null) return;
Queue<Node> q = new LinkedList<>();
q.add(node);
boolean flag = true;
while(!q.isEmpty()){
int len = q.size();
List<Node> temp = new LinkedList<>();
for (int i = 0; i < len; i++) {
Node cur = q.poll();
temp.add(cur);
if(cur.left != null){
q.add(cur.left);
}
if(cur.right != null){
q.add(cur.right);
}
}
if(flag){
for(Node n : temp){
System.out.print(n.value);
}
flag = !flag;
}else {
Collections.reverse(temp);
for (Node n : temp) {
System.out.print(n.value);
}
flag = !flag;
}
System.out.println();
}
System.out.println();
}
//根据二叉树的前序遍历和中序遍历构建二叉树
class Solution {
public Node buildTree(int[] preorder, int[] inorder) {
if (preorder == null || inorder == null || preorder.length == 0 || inorder.length == 0 || preorder.length != inorder.length)
return null;
return buildTreeHelper(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
public Node buildTreeHelper(int[] preorder , int pstart , int pend , int[] inorder , int istart, int iend){
if(pstart > pend || istart > iend) return null;
Node root = new Node(preorder[pstart]);
int index = 0;
while(inorder[istart + index] != preorder[pstart]){
index++;
}
//找准左右字节点对应的数组下标
root.left = buildTreeHelper(preorder,pstart+1,pstart+index,inorder,istart,istart +index-1);
root.right = buildTreeHelper(preorder,pstart + index + 1 , pend ,inorder , istart + index + 1 , iend);
return root;
}
}
}
树的遍历-先中后层次以及重构
最新推荐文章于 2024-09-12 19:02:20 发布