力扣-94、144、145-前中后序遍历

二叉树遍历方法总结

 二叉树的遍历总体上分为深度优先遍历和广度优先遍历。常见的前中后序三种遍历方式就属于深度优先遍历,遍历过程中是顺着一条路径一直遍历到空节点然后向上回溯继续顺着遍历上一个节点的其他方向。层序遍历属于广度优先遍历,先遍历完同一层的节点,再接着遍历下一层节点。
 本文主要介绍二叉树三种深度优先遍历方式的实现方式:递归方式和非递归方式。
 递归方式实现如下。
 前序遍历-力扣144:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        if (root == null) {
            return result;
        }
        preOrder(root,result);
        
        return result;
    }

    private void preOrder(TreeNode current,List<Integer> result) {
        if (current != null) {
            result.add(current.val);
            preOrder(current.left,result);
            preOrder(current.right,result);
        }
    }
}

 中序遍历-力扣94,就是把前序中的处理节点的顺序调整一下。

private void inOrder(TreeNode current,List<Integer> result) {
        if (current != null) {          
            inOrder(current.left,result);
            result.add(current.val);
            inOrder(current.right,result);
        }
}

 后序遍历-力扣145,将节点处理顺序调整到最后:

private void inOrder(TreeNode current,List<Integer> result) {
        if (current != null) {          
            inOrder(current.left,result);           
            inOrder(current.right,result);
            result.add(current.val);
        }
}

 非递归方式实现如下。
 前序遍历-力扣144,利用栈保存访问过的节点,每访问一个节点,就处理一个。

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

 中序遍历-力扣94

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

 后序遍历-力扣145,后序遍历可以当成是把前序遍历顺序改变一下,从前序的中左右变成中右左,然后再把结果倒置。

class Solution {
    //后序遍历
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        if (root == null) {
            return result;
        }
        Deque<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        TreeNode cur = root;
        while (!stack.isEmpty()) {
            TreeNode tempNode = stack.pop();
            result.add(tempNode.val);
            if (tempNode.left != null) {
                stack.push(tempNode.left);
            }
            if (tempNode.right != null) {
                stack.push(tempNode.right);
            }
        }
        Collections.reverse(result);
        return result;
    }

    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值