- 浏览: 188315 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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]
]
同样是二叉树广度优先遍历的变形,要求从下面开始记录每一层的节点,我们可以从根节点开始,倒序记录每一层的节点就可以了。代码如下:
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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 288Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 292You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 413Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 395Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 521Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 599Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 503Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 696Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 495The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 450Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 611Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 625Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 451All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 924Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 950Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 625Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 715Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 895Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 808You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 749For a undirected graph with tre ...
相关推荐
2. 在LeetCode平台上,问题编号107的题目是“Binary Tree Level Order Traversal II”,它要求对给定的二叉树进行反向层级遍历,即从最后一层到第一层的顺序输出节点值。 3. 反向层级遍历算法可以通过先进行正常的...
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same ...Binary Tree Level Order Traversal II.js108 Convert Sorted Array to Binary Search Tr
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
由于LeetCode平台提供了一个公共的编程问题库供开发者练习和提升编程能力,因此我们选择了其中的题目编号102,即"Binary Tree Level Order Traversal"(二叉树的层序遍历)。为了解决这个问题,我们通常需要掌握队列...
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add ...Level Order Traversal II], Tree/BFS 2017.06.20 打卡[LeetCode 324. Wiggle Sort II], S
Now we have a serial of numbers. Please build a Binary Search Tree with them. Then output its level-order traversal.
通过LeetCode题解的这个文件“js-leetcode题解之107-binary-tree-level-order-traversal-ii.js”,我们可以学习如何使用JavaScript编写二叉树层序遍历的变种,以及如何处理和操作队列数据结构。
从文件名称“Binary_Tree_Level_Order_Traversal_II”来看,这可能涉及到层次遍历的变种或扩展问题。例如,可能需要实现非递归版本的层次遍历,或者在层次遍历的基础上解决更复杂的问题,比如找出二叉树每一层的节点...
102-Binary Tree Level Order Traversal199-Binary Tree Right Side View:层次遍历的一个运用树的构造给出前中后序的序列中的两个,构造一棵树。递归。前序 parent left-child right-child中序 left-child parent ...
在给定的代码中,我们讨论的是如何解决LeetCode上的一个问题——“二叉树的层次遍历II”(Binary Tree Level Order Traversal II),这个问题要求我们实现一个C++函数`levelOrderBottom`,该函数接收一个二叉树的根...
Traversal II 144 二叉树的前序遍历 145 二叉树的后序遍历 150 逆波兰表达式求值 167 两数之和 II - 输入有序数组 199 二叉树的右视图 每天一算:Binary Tree Right Side View 203 移除链表元素 206 反转
今天我们将详细讨论Python实现LeetCode第103题——二叉树的锯齿形层序遍历(Binary Tree Zigzag Level Order Traversal)的解决方案。 二叉树的锯齿形层序遍历要求我们以不同于传统层序遍历的方式输出树节点值,即...
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和...
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 - ...
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下载 ...Traversal II 136 只出现一次的数字 2019-01-16 144 二叉树的前序遍历 145 二叉树的后序遍历 146 LRU缓存机制 LRU缓存机制 2019-01-25 Made by Jun chen 150 逆波兰表达式求值 167 两数之
function levelOrder(root) { if (!root) return []; let result = []; let queue = [root]; while (queue.length > 0) { let levelSize = queue.length; let level = []; for (let i = 0; i < levelSize;...