首页 > 代码库 > 二叉树的先序、中序、后序、层次遍历的递归和非递归解法
二叉树的先序、中序、后序、层次遍历的递归和非递归解法
二叉树的先序、中序、后序、层次遍历的递归和非递归解法
package tree; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class TreeTraverse { /** * 先序递归 * @param root */ public static void preOrderTraverse(TreeNode root) { if (root == null) { return; } System.out.print(root.val + " "); preOrderTraverse(root.left); preOrderTraverse(root.right); } /** * 先序非递归 * @param root */ public static void preOrderTraverseNoR(TreeNode root) { if (root == null) { return; } Stack<TreeNode> st = new Stack<TreeNode>(); TreeNode pNode = root; while (pNode != null || !st.isEmpty()) { if (pNode != null) { System.out.print(pNode.val + " "); st.push(pNode); pNode = pNode.left; } else { pNode = st.pop(); pNode = pNode.right; } } } /** * 中序递归 * @param root */ public static void inOrderTraverse(TreeNode root) { if (root == null) { return; } inOrderTraverse(root.left); System.out.print(root.val + " "); inOrderTraverse(root.right); } /** * 中序非递归 * @param root */ public static void inOrderTraverseNoR(TreeNode root) { if (root == null) { return; } Stack<TreeNode> st = new Stack<TreeNode>(); TreeNode pNode = root; while (pNode != null || !st.isEmpty()) { if (pNode != null) { st.push(pNode); pNode = pNode.left; } else { pNode = st.pop(); System.out.print(pNode.val + " "); pNode = pNode.right; } } } /** * 中序递归 * @param root */ public static void postOrderTraverse(TreeNode root) { if (root == null) { return; } postOrderTraverse(root.left); postOrderTraverse(root.right); System.out.print(root.val + " "); } /** * 后序非递归 * @param root */ public static void postOrderTraverseNoR(TreeNode root) { if (root == null) { return; } Stack<TreeNode> st = new Stack<TreeNode>(); st.push(root); TreeNode pNode = null; TreeNode preNode = null; while (!st.isEmpty()) { pNode = st.peek(); // 如果当前节点没有子结点或者子结点被访问过 if ((pNode.left == null && pNode.right == null) || (preNode != null && (pNode.left == preNode || pNode.right == preNode))) { System.out.print(pNode.val + " "); preNode = st.pop(); } else { if (pNode.right != null) { st.push(pNode.right); } if (pNode.left != null) { st.push(pNode.left); } } } } /** * 层次遍历 * @param root */ public static void levelOrderTraverse(TreeNode root) { if (root == null) { return; } Queue<TreeNode> q = new LinkedList<TreeNode>(); q.offer(root); TreeNode pNode = null; while (!q.isEmpty()) { pNode = q.poll(); System.out.print(pNode.val + " "); if (pNode.left != null) { q.offer(pNode.left); } if (pNode.right != null) { q.offer(pNode.right); } } } public static void main(String[] args) { TreeNode node0 = new TreeNode(0); 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 node8 = new TreeNode(8); node1.left = node0; node4.left = node2; node4.right = node6; node2.left = node1; node2.right = node3; node6.left = node5; node6.right = node7; node7.right = node8; preOrderTraverse(node4); System.out.println(); preOrderTraverseNoR(node4); System.out.println(); inOrderTraverse(node4); System.out.println(); inOrderTraverseNoR(node4); System.out.println(); postOrderTraverse(node4); System.out.println(); postOrderTraverseNoR(node4); System.out.println(); levelOrderTraverse(node4); } }
二叉树的先序、中序、后序、层次遍历的递归和非递归解法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。