Java实现二叉树的递归、非递归,前序遍历、中序遍历、后序遍历;以及层次遍历

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值