leetcode习题:后序遍历二叉树

问题描述

后序遍历二叉树(题目来源于leetcode)
后序遍历二叉树
模板为

/**
 * 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> postorderTraversal(TreeNode root) {
    
    }
}

我的思路

使用stack控制访问的顺序

从根节点出发,依次遍历左子树,加入stack中

使用hashMap记入分支节点的访问次数

每一个分支节点应当访问两次,且第一次访问时,不能直接加入list中,需等到第二次才可以

LinkedList<Integer> list = new LinkedList<>();
        if(root==null)return list;

        //postorder-traversal:left--right---root
        //使用栈控制访问顺序
        //从根节点出发,依次遍历左子树,加入stack中
        Stack<TreeNode> stack = new Stack<>();
        while(root!=null){
            stack.add(root);
            root=root.left;
        }

        //使用hashMap记入分支节点的访问次数
        HashMap<TreeNode, Integer> hashMap = new HashMap<>();
        while(!stack.isEmpty()){
            TreeNode elem = stack.peek();
            //elem右孩子为空,直接加入list,无需考虑访问次数
            if(elem.right==null)list.add(stack.pop().val);
            else{
            //3. 第二次访问elem,则加入list中
                if(hashMap.containsKey(elem)){
                    list.add(stack.pop().val);
                }
            }
            //1. 首次访问分支节点elem,且hashmap中没有记入
            if(elem.right!=null&&!hashMap.containsKey(elem)){
                //2. 第一次访问,加入hashmap
                hashMap.put(elem,1);
                //从当前节点出发,依次遍历左子树,加入stack中
                TreeNode node = elem.right;
                while (node != null) {
                    stack.add(node);
                    node=node.left;
                }
            }
        }

        return list;

leetcode优解

看完真可谓是巧夺天工!!!
大概是40300kB位置

仅仅使用一个栈

从根节点出发,依次遍历右子树(直接反过来),加入stack中,并直接加入list中

对于栈顶元素处理

判断有无左孩子,如果有,从左孩子出发,依次遍历右子树(直接反过来),加入stack中,并直接加入list中

最终来个翻转

        List<Integer> res= new ArrayList<Integer>();
        Deque<TreeNode> stack = new ArrayDeque<TreeNode>();

        while(!stack.isEmpty() || root != null){

            while(root != null){
                stack.offerLast(root);
                res.add(root.val);
                root = root.right;

            }
            root = stack.pollLast();
            root = root.left;
        }
        Collections.reverse(res);
        return res;

例子说明

在这里插入图片描述

执行玩第一个while循环

在这里插入图片描述

root = stack.pollLast();

root=6

root = root.left;

root=null
在这里插入图片描述

执行第二次循环时,由于root==null,循坏直接退出

root = stack.pollLast();

root=3

root = root.left;

root=null
在这里插入图片描述

执行第三次循环时

root = stack.pollLast();

root=1

root = root.left;

root=2

第三次循环结束后

在这里插入图片描述

root = stack.pollLast();

root=2

root = root.left;

root=4

执行完第四次循环后

在这里插入图片描述

第五次循环

root = stack.pollLast();

root=4

root = root.left;

root=null------》结束循环

第六次循环

root = stack.pollLast();

root=4

root = root.left;

root=null------》结束循环

此时list

在这里插入图片描述
再翻转,perfect!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值