二叉树的三种遍历实现方式与三种实现方式

方式一:
采用链表的方式进行书写,思想为递归的方式

TreeNode
public class TreeNode {
	//节点值
	int data;
	TreeNode leftChild;
	TreeNode rightChild;
	
	public TreeNode(int data) {
		this.data = data;
	}

}
import java.util.Arrays;
import java.util.LinkedList;

public class TwoTree {
	public static TreeNode createBinaryTree(LinkedList<Integer> inputList) {
		TreeNode node = null;
		// 判断节点值是否为空,若为空则将空值返回
		if (inputList == null || inputList.isEmpty()) {
			return null;
		}
		// 这里很关键,如果元素为空,则不再进一步递归,移除节点值
		Integer date = inputList.removeFirst();
		// 如果不为空将节点值进行递归
		if (date != null) {
			node = new TreeNode(date);
			node.leftChild = createBinaryTree(inputList);
			node.rightChild = createBinaryTree(inputList);
		}
		return node;
	}

	/**
	 * 前序遍历 根节点 左孩子 右孩子
	 * 
	 * @param node
	 */
	public static void preOrderTraveral(TreeNode node) {
		// 如果节点值为空,则将其关闭
		if (node == null) {
			return;
		}
		System.out.println(node.data);
		preOrderTraveral(node.leftChild);
		preOrderTraveral(node.rightChild);
	}

	/**
	 * 中序遍历 左孩子 根节点 右孩子
	 * 
	 * @param node
	 */
	public static void inOrderTraveral(TreeNode node) {
		if (node == null) {
			return;
		}
		inOrderTraveral(node.leftChild);
		System.out.println(node.data);
		inOrderTraveral(node.rightChild);
	}

	/**
	 * 后序遍历 左孩子 右孩子 根节点
	 * 
	 * @param node
	 */
	public static void postOrderTraveral(TreeNode node) {
		if (node == null) {
			return;
		}
		postOrderTraveral(node.leftChild);
		postOrderTraveral(node.rightChild);
		System.out.println(node.data);
	}

	public static void main(String[] args) {
         //asList()恰巧可用于将数组转为集合。
		LinkedList<Integer> inputList = new LinkedList<Integer>(
				Arrays.asList(new Integer[] { 3, 2, 9, null, null, 10, null, null, 8, null, 4 }));
		TreeNode treeNode = createBinaryTree(inputList);
		System.out.println("前序遍历:");
		preOrderTraveral(treeNode);
		System.out.println("中序遍历:");
		inOrderTraveral(treeNode);
		System.out.println("后序遍历:");
		postOrderTraveral(treeNode);
	}
}

方式二:
如果采用的方式进行书写(入栈与出栈
举例:前序遍历

import java.util.Stack;
public class StackTree {
	
	/**
	 * 二叉树非递归前序遍历
	 * 采用栈的方式进行
	 * @param root
	 */
	public static void preOrderTraveralWithStack(TreeNode root) {
		Stack<TreeNode> stack = new Stack<TreeNode>();
		TreeNode treeNode = root;
		while (treeNode != null || stack.isEmpty()) {
			//迭代访问节点的左孩子,并入栈
			while (treeNode != null) {
				System.out.println(treeNode.data);
				stack.push(treeNode);
				treeNode = treeNode.leftChild;
			}
			//如果节点没有左孩子,刚弹出栈顶节点,访问节点右孩子
			if (!stack.isEmpty()) {
				treeNode = stack.pop();
				treeNode = treeNode.rightChild;
			}
		}		
	}
}

方式三:
二叉树的层序遍历广度优先
采用队列的方式进行排序

import java.util.LinkedList;
import java.util.Queue;
public class Ground {
	private static void levelOrderTraversal(TreeNode root) {
		Queue<TreeNode> queue = new LinkedList<TreeNode>();
		queue.offer(root);
		while (!queue.isEmpty()) {
			TreeNode node = queue.poll();
			System.out.println(node.data);
			if (node.leftChild != null) {
				queue.offer(node.leftChild);
			}
			if (node.rightChild != null) {
				queue.offer(node.rightChild);
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrl精

面试很多问题,积攒不容易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值