二叉树的前、中、后序遍历

总结:二叉树的前中后序遍历递归实现属于easy题,非递归实现属于midium或hard题,后续遍历非递归比较难懂,题解看不懂,就找视频看不懂的点,然后自己演算一遍,就比较好懂

递归实现:

前序遍历   根左右

/**
 * 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 static void preOrderRecur(TreeNode head) {
    if (head == null) {
        return;
    }
    System.out.print(head.value + " ");
    preOrderRecur(head.left);
    preOrderRecur(head.right);
}
}

中序遍历       左根右

class Solution {

    public static void preOrderRecur(TreeNode head) {
    if (head == null) {
        return;
    }
    preOrderRecur(head.left);
    System.out.print(head.value + " ");
    preOrderRecur(head.right);
}
}

后序遍历       左右根

class Solution {

    public static void preOrderRecur(TreeNode head) {
    if (head == null) {
        return;
    }
    preOrderRecur(head.left);
    preOrderRecur(head.right);
    System.out.print(head.value + " ");
}
}

非递归实现(迭代解法)

前序遍历:本质上是在模拟递归,因为递归也是调用了系统栈

思路:后进先出,先压入右节点,再压入左节点

class Solution {

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root==null)
        return list;
        Stack<TreeNode> stack = new Stack();
        stack.add(root);
        while(!stack.isEmpty())
        {
            TreeNode node = stack.pop();
            list.add(node.val);
            if(node.right!=null)
                stack.push(node.right);
            if(node.left!=null)
                stack.push(node.left);            
        }
        return list;
}
}

中序遍历:

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
    while(root!=null || !stack.isEmpty())
    {
        while(root!=null)//根节点左边递归压入
        {
            if(root!=null)
            stack.push(root);
            root=root.left;
        }
        //左边到底,取出根节点,判断其右节点,对右节点进行左递归压入,最外层while的由来
        //循环条件  节点存在但未压入,节点不存在,但栈中仍有节点
        root=stack.pop();
        list.add(root.val);
        root=root.right;
    }
    return list;

    }
}

什么是二叉树的中序遍历?遍历从左孩子节点开始,依次是左中右,如果左孩子还有孩子节点,则进行递归遍历,遍历顺序依然是左、中、右

先想清楚,或者草稿纸演算出整个过程,再写代码 

 后序遍历【比较正经的遍历方式】

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> list = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            TreeNode preNode=null;
            while(root!=null||!stack.isEmpty())
            {
                 while(root!=null)
                {
                    stack.push(root);
                    root=root.left;
                }
                root=stack.pop();
                if(root.right==null || root.right==preNode)
                {
                    list.add(root.val);
                    preNode=root;
                    root=null;
                }
                else
                {
                    stack.push(root);
                    root=root.right;
                }
            }
            return list;
    }
}

不正经的后序遍历方式,通过改变前序遍历的方式  使其为根右左,再将集合翻转即为左右根

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root==null)
        return list;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty())
        {
            root=stack.pop();
            list.add(root.val);
            if(root.left!=null)
            stack.push(root.left);
            if(root.right!=null)
            stack.push(root.right);
        }
        Collections.reverse(list);
        return list;
    }
}

144. 二叉树的前序遍历

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_40396568

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值