回顾:树的几种迭代遍历方式
Posted boy_nihao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了回顾:树的几种迭代遍历方式相关的知识,希望对你有一定的参考价值。
PS:文章最后附上完整代码
一、中序遍历
List<Integer> inOrder(BTree root)
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty())
while (null != root)
stack.push(root);
root = root.left;
node = stack.pop();
result.add(node.val);
if (null != node.right)
root = node.right;
return result;
二、前序遍历
List<Integer> preOrder(BTree root)
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty())
while (null != root)
result.add(root.val);
stack.push(root);
root = root.left;
node = stack.pop();
if (null != node.right)
root = node.right;
return result;
三、后序遍历
List<Integer> postOrder(BTree root)
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty())
while (null != root)
result.add(0, root.val);
stack.push(root);
root = root.right;
node = stack.pop();
if (null != node.left)
root = node.left;
return result;
四、层次遍历
List<Integer> layerOrder(BTree root)
List<Integer> result = new ArrayList<>();
if (null == root)
return result;
Queue<BTree> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty())
BTree node = queue.poll();
if (null != node.left)
queue.offer(node.left);
if (null != node.right)
queue.offer(node.right);
result.add(node.val);
return result;
测试代码:
import java.util.*;
/**
* Copyright © 2018 Chris. All rights reserved.
*
* @author Chris
*/
public class Solution
static class BTree
int val;
BTree left, right;
BTree(int v) val = v;
@Override
public String toString()
return val + "";
/**
* 层次遍历
*
* @param root 根结点
* @return 遍历结果
*/
List<Integer> layerOrder(BTree root)
List<Integer> result = new ArrayList<>();
if (null == root)
return result;
Queue<BTree> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty())
BTree node = queue.poll();
if (null != node.left)
queue.offer(node.left);
if (null != node.right)
queue.offer(node.right);
result.add(node.val);
return result;
/**
* 后序遍历
*
* @param root 根结点
* @return 遍历结果
*/
List<Integer> postOrder(BTree root)
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty())
while (null != root)
result.add(0, root.val);
stack.push(root);
root = root.right;
node = stack.pop();
if (null != node.left)
root = node.left;
return result;
/**
* 前序遍历
*
* @param root 根结点
* @return 遍历结果
*/
List<Integer> preOrder(BTree root)
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty())
while (null != root)
result.add(root.val);
stack.push(root);
root = root.left;
node = stack.pop();
if (null != node.right)
root = node.right;
return result;
/**
* 中序遍历
*
* @param root 根结点
* @return 遍历结果
*/
List<Integer> inOrder(BTree root)
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty())
while (null != root)
stack.push(root);
root = root.left;
node = stack.pop();
result.add(node.val);
if (null != node.right)
root = node.right;
return result;
public static void main(String args[])
Solution s = new Solution();
BTree root = new BTree(1);
BTree left = new BTree(2);
BTree right = new BTree(3);
left.left = new BTree(4);
left.right = new BTree(5);
right.left = new BTree(6);
right.right = new BTree(7);
root.left = left;
root.right = right;
System.out.println(s.inOrder(root));
System.out.println(s.preOrder(root));
System.out.println(s.postOrder(root));
System.out.println(s.layerOrder(root));
以上是关于回顾:树的几种迭代遍历方式的主要内容,如果未能解决你的问题,请参考以下文章