`

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]
]

同样是二叉树广度优先遍历的变形,要求从下面开始记录每一层的节点,我们可以从根节点开始,倒序记录每一层的节点就可以了。代码如下:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> result = new LinkedList<List<Integer>>();
        List<Integer> list = new ArrayList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if(root == null)
            return result;
        queue.offer(root);
        int helper = 1, count = 0;
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            list.add(cur.val);
            helper --;
            if(cur.left != null) {
                queue.offer(cur.left);
                count ++;
            }
            if(cur.right != null) {
                queue.offer(cur.right);
                count ++;
            }
            if(helper == 0) {
                result.addFirst(new ArrayList<Integer>(list));
                helper = count;
                count = 0;
                list.clear();
            }
        }
        return result;
    }
}
分享到:
评论

相关推荐

    【LeetCode】102. Binary Tree Level Order Traversal

    我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve

    cpp-算法精粹

    Binary Tree Level Order Traversal II Binary Tree Right Side View Invert Binary Tree Binary Search Tree Iterator Binary Tree Zigzag Level Order Traversal Recover Binary Search Tree Same Tree Symmetric ...

    leetcode卡-LeetCode:我的LeetCode解决方案

    leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add ...Level Order Traversal II], Tree/BFS 2017.06.20 打卡[LeetCode 324. Wiggle Sort II], S

    Level Order Traversal

    Now we have a serial of numbers. Please build a Binary Search Tree with them. Then output its level-order traversal.

    leetcode-tree

    102-Binary Tree Level Order Traversal199-Binary Tree Right Side View:层次遍历的一个运用树的构造给出前中后序的序列中的两个,构造一棵树。递归。前序 parent left-child right-child中序 left-child parent ...

    leetcode中文版-LeetCodeAnimation:力码动画

    Traversal II 144 二叉树的前序遍历 145 二叉树的后序遍历 150 逆波兰表达式求值 167 两数之和 II - 输入有序数组 199 二叉树的右视图 每天一算:Binary Tree Right Side View 203 移除链表元素 206 反转

    leetcode-[removed]使用Java的Leetcode解决方案

    102 Binary Tree Level Order Traversal.js(二叉树级订单Traversal.js) 103 Binary Tree Zigzag Level Order Traversal.js(二叉树之字形级别顺序Traversal.js) 104 Binary Tree.js的最大深度 105从Preorder和...

    javalruleetcode-what_the_dead_men_say:what_the_dead_men_say

    Binary Search Tree - Java Recursive - Java Iterative - Java Inorder 0099 Recover Binary Search Tree - Java Recursive 0101 Symmetric tree - Java Recursive - Java Iterative - C Recursive - ...

    lrucacheleetcode-luoleet:LeetcodesolutionsbyXinhangLuoandQinghaoDai

    https://leetcode.com/problems/binary-tree-level-order-traversal/ Binary Tree Level Order Traversal 103 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Binary Tree Zigzag Level ...

    leetcode下载-LeetCodeAnimation:力码动画

    leetcode下载 ...Traversal II 136 只出现一次的数字 2019-01-16 144 二叉树的前序遍历 145 二叉树的后序遍历 146 LRU缓存机制 LRU缓存机制 2019-01-25 Made by Jun chen 150 逆波兰表达式求值 167 两数之

    陈越、何钦铭-数据结构作业16:Complete Binary Search Tree完全二叉搜索树

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a... You are supposed to output the level order traversal sequence of this BST.

    CBST.rar_The Keys_bst

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a... You are supposed to output the level order traversal sequence of this BST.

    lrucacheleetcode-LeetCode_Note:leetcode个人笔记

    lru缓存leetcode LeetCode_Note leetcode 个人笔记 问题清单 [1_two_sum.cpp] [10_regular-expression-matching.cpp] [100_same-tree.cpp] ...[107_binary-tree-level-order-traversal-ii.cpp] [108_convert-sorted

    react-binary-tree:二叉树遍历可视化

    Level Order Traversal 2. Depth First Traversal(Pre-order,Post-order,In-order) 您可以在此处了解有关这些算法的更多信息: 运行项目 1. Clone the repo. 2. Install dependencies using `npm install` or `yarn...

    四平方和定理leetcode-leetcode-practice:个人LeetCode练习代码

    102.binary-tree-level-order-traversal (二叉树的层序遍历) 104.maximum-depth-of-binary-tree (二叉树的最大深度) 105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造...

    Binary-Search-Tree-Visualizer:大家好……我用 Tkinter 的 Python 制作了一个二叉搜索树(BST)可视化工具

    Level-Order-Traversal-Logic-Visualization 使用循环队列 :light_bulb: 如果你喜欢这个项目,请点击星星并在 GitHub 上关注我以获取新的项目更新 点击此处查看项目视频 在 LinkedIn 上关注我以获取定期项目更新

    leetcode中文版-leetcode:leetcode

    leetcode中文版车鸟 一组用python编写的算法。 种类 冒泡排序 插入排序 归并排序 桶排序 计数排序 基数排序 ...balance_binary_tree ...best_time_to_buy_and_sell_stock_II ...binary_tree_level_order_traversal_I

    leetcode中文版-cabbird:一组用python编写的算法

    leetcode中文版车鸟 一组用python编写的算法。 种类 冒泡排序 插入排序 归并排序 桶排序 计数排序 基数排序 ...balance_binary_tree ...best_time_to_buy_and_sell_stock_II ...binary_tree_level_order_traversal_I

    数据结构二叉树功能展示

    cout&lt;&lt;" traversal in levelorder ... 广度优先遍历二叉树"; cout求某结点的双亲"; cout&lt;&lt;" calculate the depth of the tree ... 求该树的深度"; cout&lt;&lt;" calculator the leaf of the tree ... 求该树叶子...

    leetcodetreenode-Leetcode:包含已解决的Leetcode问题的存储库

    leetcode 树节点力码 ...binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). Runtime: 8 ms, faster than 100.00% of C++ online submi

Global site tag (gtag.js) - Google Analytics