107-Binary Tree Level Order Traversal II
Description
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,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
問題描述
給定二叉樹, 返回其由底向上的層級遍歷的值序列
問題分析
只需要將層級遍歷後的值序列通過集合框架的reverse逆序即可
需要注意利用depth
解法
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList();
if(root == null) return res;
dfs(root, 0, res);
Collections.reverse(res);
return res;
}
public void dfs(TreeNode root, int depth, List<List<Integer>> res) {
if(root == null) return;
if(depth == res.size()) res.add(new ArrayList());
res.get(depth).add(root.val);
dfs(root.left, depth + 1, res);
dfs(root.right, depth + 1, res);
}
}
相關文章
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- Binary-tree-level-order-traversal
- 102-Binary Tree Level Order Traversal
- 103. Binary Tree Zigzag Level Order Traversal
- [LintCode] Binary Tree Level Order
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 145. Binary Tree Postorder TraversalLeetCode
- LeetCode之Increasing Order Search Tree(Kotlin)LeetCodeKotlin
- PG12-2 B-Tree 索引 level 0 root頁索引
- PG 12-2 B-Tree 索引 分析 分裂 level = 1索引
- P1501 [國家集訓隊]Tree II
- The order of a Tree (二叉搜尋樹+先序遍歷)
- Traversal of trees
- P10013 [集訓隊互測 2023] Tree Topological Order Counting
- Server-side vulnerabilities :path traversalServerIDE
- Level Up
- CSS orderCSS
- 記一次kafka的high level和low levelKafka
- Order by 優化優化
- order by與索引索引
- natas(level0-level14)通關詳細指南(一)
- E. Level Up
- E - Level K Palindrome
- CSS LEVEL4CSS
- Range Addition II 範圍求和 II
- [Javascript] Object property orderJavaScriptObject
- MySQL 之 ORDER BY FIELDMySql
- mysql order by 優化MySql優化
- CSS3 orderCSSS3
- tree
- BUU_jarvisoj_level0JAR
- RuneScape - To verify your level of combatBAT
- FUNBOX-5: NEXT LEVEL