zoukankan      html  css  js  c++  java
  • LeetCode 145. 二叉树的后序遍历

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            //一般解法,前序遍历后,翻转下结果集,注意下 与前序遍历的进栈顺序不一样
            //(前序) 根左右 --> 变为 根右左 --> 翻转 左右根 (后续)
            //定义一一个栈
            Stack<TreeNode> stack = new Stack<>();
            //定义返回的结果集
            List<Integer> res = new LinkedList();
     
            if(root == null) return res;
            //根节点入栈
            stack.push(root);
             
            while(!stack.isEmpty()){
                TreeNode node = stack.pop();
                res.add(node.val);
                if(node.left != null) stack.push(node.left);
                if(node.right != null) stack.push(node.right);    
            }
            Collections.reverse(res);
            return res;
        }
    }
  • 相关阅读:
    File
    多态
    方法重载
    Math
    instanceof
    强制类型转换
    泛型
    springboot热部署
    iOS bug处理
    iOS8-xcode6中添加pch全局引用文件
  • 原文地址:https://www.cnblogs.com/peanut-zh/p/13881778.html
Copyright © 2011-2022 走看看