[LeetCode/LintCode] Binary Tree Postorder Traversal

news/2024/6/18 5:50:19

Problem

Given a binary tree, return the postorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

不用递归吗?没问题,我们用LinkedList做,速度惊人。对于左子树,放入链表;对于右子树,直接移动nodenode = node.right。这样每次用addFirst()node放入结果数组的首位,再将root.right放入首位,每次再将root.right的左子树放入链表,当右子树遍历完后,再从链表中以FIFO的顺序取出从上到下的左子树结点,以相同方法放入首位。

Solution

Using LinkedList

public class Solution {
    public List<Integer> postorderTraversal(TreeNode node) {
        LinkedList<Integer> result = new LinkedList<Integer>();
        Stack<TreeNode> leftChildren = new Stack<TreeNode>();
        while(node != null) {
            result.addFirst(node.val);
            if (node.left != null) leftChildren.push(node.left);
            node = node.right;
            if (node == null && !leftChildren.isEmpty()) node = leftChildren.pop();
        }
        return result;
    }
}

Using Recursion

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

http://www.niftyadmin.cn/n/2422879.html

相关文章

mysql tcmalloc_技术分享 | tcmalloc解决mysqld实例引发的cpu过高问题

原创&#xff1a;任坤作者简介任坤&#xff0c;现居珠海&#xff0c;先后担任专职 Oracle 和 MySQL DBA&#xff0c;现在主要负责 MySQL、mongoDB 和 Redis 维护工作。背景MySQL 版本&#xff1a;5.6.29&#xff0c;普通主从OS&#xff1a;CentOS 6.8最近一段时间线上某实例频繁…

java连接mysql数据库教程视频_有没有Java教学视频讲如何jdbc链接mysql数据库的的...

展开全部一般的java web编程都会有jdbc编程教32313133353236313431303231363533e59b9ee7ad9431333337626133程&#xff0c;连接mysql oracle 等基本都是一样的。java数据库编程要用JDBCJDBC用法很简单,创建一个以JDBC连接数据库的程序&#xff0c;包含7个步骤&#xff1a;1、加…

[读书笔记]机器学习:实用案例解析(1)

第1章 使用R语言 #machine learing for heckers #chapter 1 library(ggplot2) library(plyr)#.tsv文件用制表符进行分割#字符串默认为factor类型&#xff0c;因此stringsAsFactors置FALSE防止转换#header置FALSE防止将第一行当做表头#定义空字符串为NA&#xff1a;na.strings …

使用QEMU调试Linux内核代码

http://blog.chinaunix.net/uid-20729583-id-1884617.html http://www.linuxidc.com/Linux/2014-08/105510.htm Linux内核代码的调试非常麻烦&#xff0c;一般都是加printk, 或者用JTAG调试。这里的方法是用QEMU来调试Linux内核。因为QEMU自己实现了一个gdb server, 所以可以非…

MongoCola使用教程 2 - MongoDB的Replset 初始化和配置

前言 首先再次感谢博客园的各位朋友。正是你们的关注才让我有信心将这个工具开发下去。 这周同样也有热心网友对于MongoCola存在的问题给予了反馈。 这次工具更新到了版本1.20&#xff0c;强化的地方是增加了Replset和Sharding的管理能力。MongoVUE和Mongocola以前在显示一个R…

Oracle RMAN 的 show,list,crosscheck,delete命令整理

1、SHOW命令&#xff1a;显示rman配置&#xff1a; RMAN> show all;2、REPORT命令&#xff1a;2.1、RMAN> report schema 报告目标数据库的物理结构;2.2、RMAN> report need backup days3/days 3; 报告最近3天没有被备份的数据文件&#xff1b;2.3、RMAN> report n…

python turtle画笑脸_如何用python画笑脸QQ表情——turtle库实践

from turtle import *screensize(600,600)speed(10)#笑脸的小圆脸pensize(5)color(dim grey,yellow)pu()goto(0,-100)begin_fill()circle(100)end_fill()#腮红#左侧seth(90)color(Light Pink,Light Pink)pu()goto(-55,-5)pd()begin_fill()circle(20)end_fill()#右侧color(Light…

maven项目迁入内网的各个坑

前言&#xff1a;我之前做的一个项目一直是在内网环境&#xff0c;进行开发的时候是在外网开发好了后打包传入内网。有许多的不便 因此我整个项目迁入内网才内网开发&#xff0c;琢磨了好一会才找到各个问题的解决方案。最近公司新进了一个新同事 然后让我带带&#xff0c;这就…