新博文地址:
[leetcode]Binary Tree Level Order Traversal II
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],
]
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],
]
实在看不出来跟Binary Tree Level Order Traversa有啥本质区别。。。所以我偷懒,在原来的基础上加了一个stack。。。。算法完全一样。o(╯□╰)o,接受大家的鄙视。
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); Stack<ArrayList<Integer>> stack = new Stack<ArrayList<Integer>>(); public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) { if(root == null){ return result; } ArrayDeque<TreeNode> pre = new ArrayDeque<TreeNode>(); ArrayDeque<TreeNode> post= new ArrayDeque<TreeNode>(); pre.offer(root); ArrayList<Integer> rootList = new ArrayList<Integer>(Arrays.asList(root.val)); stack.push(rootList); while(!pre.isEmpty()){ TreeNode tem = pre.poll(); if(tem.left!=null){ post.offer(tem.left); } if(tem.right!=null){ post.offer(tem.right); } if(pre.isEmpty()){ if(post.isEmpty()){ break; } pre = post.clone(); post.clear(); ArrayList<Integer> list = new ArrayList<Integer>(); for(TreeNode node : pre){ list.add(node.val); } stack.push(list); } } while(!stack.isEmpty()){ ArrayList<Integer> list = stack.pop(); result.add(list); } return result; }
相关推荐
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
python python_leetcode题解之107_Binary_Tree_Level_Order_Traversal_II
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
python python_leetcode题解之102_Binary_Tree_Level_Order_Traversal
js js_leetcode题解之102-binary-tree-level-order-traversal.js
python python_leetcode题解之103_Binary_Tree_Zigzag_Level_Order_Traversal
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...
js js_leetcode题解之103-binary-tree-zigzag-level-order-traversal.js
102-Binary Tree Level Order Traversal199-Binary Tree Right Side View:层次遍历的一个运用树的构造给出前中后序的序列中的两个,构造一棵树。递归。前序 parent left-child right-child中序 left-child parent ...
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和...
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 what_the_dead_men_say 所以这只是一个 repo,我从leetcode.com存储我的问题解决方案。 二叉树 0098 Validate Binary Search Tree - Java Recursive - Java Iterative - Java Inorder 0099 Recover ...
leetcode中文版 我会尽力将LeetCode上所有的题目都用动画的形式演示出来,计划用3到4年时间去完成它,期待与你见证这一天! 文章最新首发于微信公众号 五分钟学算法 ,您可以关注获取最新的文章。 Problems ID ...
- **Binary Tree Level Order Traversal**:二叉树的层次遍历。 - **Symmetric Tree**:判断一个二叉树是否是对称的。 - **Same Tree**:判断两棵二叉树是否相同。 - **Balanced Binary Tree**:判断一个二叉树...
leetcode下载 我会尽力将LeetCode上所有的题目都用动画的形式演示出来,计划用3到4年时间去完成它,期待与你见证这一天! 文章最新首发于微信公众号 五分钟学算法 ,您可以关注获取最新的文章。 Problems ID Problem...
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
leetcode中文版车鸟 一组用python编写的算法。 种类 冒泡排序 插入排序 归并排序 桶排序 计数排序 基数排序 ...balance_binary_tree ...best_time_to_buy_and_sell_stock_II ...binary_tree_level_order_traversal_I