博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binary Tree Level Order Traversal II
阅读量:4074 次
发布时间:2019-05-25

本文共 1335 字,大约阅读时间需要 4 分钟。

Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree {3,9,20,#,#,15,7},

3   / \  9  20    /  \   15   7

return its bottom-up level order traversal as:

[  [15,7],  [9,20],  [3]]

confused what "{1,#,2,3}" means? 

Java代码:

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {   public static ArrayList
> levelOrderBottom(TreeNode root) { // Note: The Solution object is instantiated only once and is reused by each test case. LinkedList
> answer = new LinkedList
>(); if (root != null) { LinkedList
currentLevelNode = new LinkedList
(); currentLevelNode.add(root); while (!currentLevelNode.isEmpty()) { ArrayList
temp = new ArrayList
(currentLevelNode.size()); LinkedList
nextLevelNode = new LinkedList
(); for (TreeNode node : currentLevelNode) { temp.add(node.val); if (node.left != null) { nextLevelNode.add(node.left); } if (node.right != null) { nextLevelNode.add(node.right); } } answer.add(0, temp); currentLevelNode = nextLevelNode; } } return new ArrayList
>(answer); }}
 

转载地址:http://nnuni.baihongyu.com/

你可能感兴趣的文章
大数据入门:Hive和Hbase区别对比
查看>>
大数据入门:ZooKeeper工作原理
查看>>
大数据入门:Zookeeper结构体系
查看>>
大数据入门:Spark RDD基础概念
查看>>
大数据入门:SparkCore开发调优原则
查看>>
大数据入门:Java和Scala编程对比
查看>>
大数据入门:Scala函数式编程
查看>>
【数据结构周周练】002顺序表与链表
查看>>
C++报错:C4700:使用了非初始化的局部变量
查看>>
【数据结构周周练】003顺序栈与链栈
查看>>
C++类、结构体、函数、变量等命名规则详解
查看>>
C++ goto语句详解
查看>>
【数据结构周周练】008 二叉树的链式创建及测试
查看>>
《软件体系结构》 第九章 软件体系结构评估
查看>>
《软件体系结构》 第十章 软件产品线体系结构
查看>>
《软件过程管理》 第六章 软件过程的项目管理
查看>>
《软件过程管理》 第九章 软件过程的评估和改进
查看>>
分治法 动态规划法 贪心法 回溯法 小结
查看>>
《软件体系结构》 练习题
查看>>
《数据库系统概论》 第一章 绪论
查看>>