树的遍历

本文对树的遍历操作做个总结:

1. 前序遍历

递归实现:

public class Solution {
	public List<Integer> preorderTraversal(TreeNode root){
		List<Integer> result = new ArrayList<Integer>();
		preOrder(result, root);
		return result;
	}
	private void preOrder(List<Integer> result, TreeNode root){
		if(root == null)
			return;
		result.add(root.val);
		preOrder(result, root.left);
		preOrder(result, root.right);
	}
}
非递归实现(栈操作法):

public class Solution {
	public List<Integer> preOrderTraversal(TreeNode root){
		List<Integer> result = new ArrayList<Integer>();
		if(root == null)
			return result;
		Deque<TreeNode> stack = new LinkedList<TreeNode>();
		stack.push(root);
		while(!stack.isEmpty()){
			TreeNode node = stack.pop();
			if(node.right != null)
				stack.push(node.right);
			if(node.left != null)
				stack.push(node.left);
			result.add(node.val);
		}
		return result;
	}
}
非递归实现:

public class Solution {
    public static List<Integer> preOrderTraversal(TreeNode root){
        List<Integer> result = new ArrayList<Integer>();
        
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
        while(root != null || !stack.isEmpty()){
            if(root == null){
                root = stack.pop().right;
                continue;
            }
            result.add(root.val);
            stack.push(root);
            root = root.left;
        }
        return result;
    }
}

2. 中序遍历

递归实现:

public class Solution {
	public List<Integer> inorderTraversal(TreeNode root){
		List<Integer> result = new ArrayList<Integer>();
	        inOrder(result, root);
		return result;
	}
	private void inOrder(List<Integer> result, TreeNode root){
		if(root == null)
			return;
		inOrder(result, root.left);
		result.add(root.val);
		inOrder(result, root.right);
	}
}
非递归实现:

public class Solution {
	public static List<Integer> inOrderTraversal(TreeNode root){
		List<Integer> result = new ArrayList<Integer>();
		
		Deque<TreeNode> stack = new LinkedList<TreeNode>();
		while(root != null || !stack.isEmpty()){
			if(root == null){
				TreeNode p = stack.pop();
				result.add(p.val);
				root = p.right;
				continue;
			}
			stack.push(root);
			root = root.left;
		}
		return result;
	}
}

3. 后序遍历

LeetCode:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/

递归实现:

public class Solution {
	public List<Integer> postorderTraversal(TreeNode root){
		List<Integer> result = new ArrayList<Integer>();
		postOrder(result, root);
		return result;
	}
	private void postOrder(List<Integer> result, TreeNode root){
		if(root == null)
			return;
		postOrder(result, root.left);
		postOrder(result, root.right);
		result.add(root.val);
	}
}

非递归实现(双栈法):

public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        if(root == null)
            return result;
        Deque<TreeNode> stack1 = new LinkedList<TreeNode>();
        Deque<TreeNode> stack2 = new LinkedList<TreeNode>();
        stack1.push(root);
        while(!stack1.isEmpty()){
            TreeNode node = stack1.pop();
            if(node.left != null)
                stack1.push(node.left);
            if(node.right != null)
                stack1.push(node.right);
            stack2.push(node);
        }
        while(!stack2.isEmpty()){
            TreeNode node = stack2.pop();
            result.add(node.val);
        }
        return result;
    }
}
非递归实现:

public class Solution {
	public static List<Integer> postOrderTraversal(TreeNode root){
		List<Integer> result = new ArrayList<Integer>();
		
		Deque<TreeNode> stack = new LinkedList<TreeNode>();
		TreeNode pre = null;
		while(root != null || !stack.isEmpty()){
			if(root == null){	
				TreeNode p = stack.peek();
				if(p.right == null || pre == p.right){
					result.add(p.val);
					stack.pop();
					pre = p;
					root = null;
				}else{
					pre = root;
					root = p.right;
				}
				continue;
			}
			stack.push(root);
			pre = root;
			root = root.left;
		}
		return result;
	}
}

4. 层次遍历

以上几种遍历方式本质上都是DFS,只不过遍历过程中取值的时间不同,所以都可以采用递归及栈的算法来实现,而层次遍历本质上的BFS,所以可以采用Queue来实现,代码如下:

LeetCode: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/

public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(root == null)
            return result;
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.offer(root);
        int cur = 1;
        int next = 0;
        while(!q.isEmpty()){
            List<Integer> level = new ArrayList<Integer>();
            while(cur != 0){
                TreeNode node = q.poll();
                cur--;
                level.add(node.val);
                if(node.left != null){
                    q.offer(node.left);
                    next++;
                }
                if(node.right != null){
                    q.offer(node.right);
                    next++;
                }
            }
            result.add(level);
            cur = next;
            next = 0;
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值