Java实现二叉树的递归、非递归,前序遍历、中序遍历、后序遍历;以及层次遍历
递归前序遍历
ArrayList<Integer> list = new ArrayList();
public ArrayList<Integer> preOrder(TreeNode root) {
if (root != null) {
list.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
return list;
}
非递归前序遍历
ArrayList<Integer> list = new ArrayList();
public ArrayList<Integer> preOrder2(TreeNode root) {
Stack<TreeNode> stack = new Stack();
while (root != null || !stack.empty()) {
while (root != null) {
stack.push(root);
list.add(root.val);
root = root.left;
}
if (!stack.empty()) {
root = stack.pop();
root = root.right;
}
}
return list;
}
递归中序遍历
ArrayList<Integer> list = new ArrayList();
public ArrayList<Integer> preOrder(TreeNode root) {
if (root != null) {
preOrder(root.left);
list.add(root.val);
preOrder(root.right);
}
return list;
}
非递归中序遍历
ArrayList<Integer> list = new ArrayList();
public ArrayList<Integer> preOrder2(TreeNode root) {
Stack<TreeNode> stack = new Stack();
while (root != null || !stack.empty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
if (!stack.empty()) {
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
return list;
}
递归后序遍历(第一种解法)
ArrayList<Integer> list = new ArrayList();
public ArrayList<Integer> preOrder(TreeNode root) {
if (root != null) {
preOrder(root.left);
preOrder(root.right);
list.add(root.val);
}
return list;
}
非递归后序遍历(第二种解法:,注意每次将结点出栈的时候,看右子结点是否是前一个结点,是的话同样需要将该结点出栈)
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;
TreeNode prior = null;
ArrayList<Integer> ans = new ArrayList<Integer>(20);
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
if (cur.right == null || cur.right == prior) {
ans.add(cur.val);
prior = cur;
cur = null;
}
else {
stack.push(cur);
cur = cur.right;
}
}
}
return ans;
}
}
非递归后序遍历(第三种解法:将前序遍历变成 :中 -> 右 -> 左 然后将 list 倒序一下,出来的结果正好就是后序遍历:左 -> 右 -> 中 )
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
List<Integer> list = new ArrayList();
TreeNode prior = null;
while (!stack.isEmpty() || root != null) {
while (root != null) {
stack.push(root);
list.add(root.val);
root = root.right;
}
if (!stack.isEmpty()) {
root = stack.pop();
root = root.left;
}
}
Collections.reverse(list);
return list;
}
}
非递归层次遍历:
public ArrayList<Integer> preOrder2(TreeNode root) {
ArrayList<Integer> list = new ArrayList();
Queue<TreeNode> queue = new LinkedList();
queue.add(root);
while (root != null || !queue.isEmpty()) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
return list;
}
递归层次遍历
class Solution {
List<List<Integer>> levels = new ArrayList<List<Integer>>();
public void helper(TreeNode node, int level) {
// start the current level
if (levels.size() == level)
levels.add(new ArrayList<Integer>());
// fulfil the current level
levels.get(level).add(node.val);
// process child nodes for the next level
if (node.left != null)
helper(node.left, level + 1);
if (node.right != null)
helper(node.right, level + 1);
}
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) return levels;
helper(root, 0);
return levels;
}
}