[Lintcode] #70 二叉树的层次遍历 II
来源:互联网 发布:b2b软件有哪些 编辑:程序博客网 时间:2024/05/17 23:40
给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历)
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /* * @param root: A tree * @return: buttom-up level order a list of lists of integer */ public List<List<Integer>> levelOrderBottom(TreeNode root) { // write your code here LinkedList<List<Integer>> re = new LinkedList<>();LinkedList<TreeNode> stack = new LinkedList<>();if (root == null)return re;stack.add(root);while (!stack.isEmpty()) {int n = stack.size();List<Integer> temp = new ArrayList<>();while (n-- > 0) {TreeNode cur = stack.poll();temp.add(cur.val);if (cur.left != null)stack.add(cur.left);if (cur.right != null)stack.add(cur.right);}re.addFirst(temp);}return re; }}
阅读全文
0 0
- LintCode-二叉树的层次遍历 II
- LintCode 二叉树的层次遍历 II
- LintCode:二叉树的层次遍历 II
- 二叉树的层次遍历 II,lintcode
- 二叉树的层次遍历 II -LintCode
- Lintcode 二叉树的层次遍历 II
- lintcode-二叉树的层次遍历 II
- lintcode-二叉树的层次遍历II-70
- LintCode 70 二叉树的层次遍历 II
- [Lintcode] #70 二叉树的层次遍历 II
- [Lintcode]Binary Tree Level Order Traversal II 二叉树的层次遍历 II
- LintCode 二叉树的层次遍历
- lintcode-二叉树的层次遍历-69
- LintCode:二叉树的层次遍历
- LintCode:二叉树的锯齿形层次遍历
- LintCode 二叉树的锯齿形层次遍历
- Lintcode(M) 二叉树的层次遍历
- 二叉树的锯齿形层次遍历,lintcode
- MiKiShow
- vs使用mfc类向导重写父类
- SpringMVC的搭建
- JAVASE入门---我对强转与多态的理解
- [C# 网络编程系列]专题五:TCP编程
- [Lintcode] #70 二叉树的层次遍历 II
- 嵌入式 uboot命令及内核启动参数
- postgres数据库查看用户权限
- C语言系列之指针类型内存分配模板
- Tomcat高性能调优方案详解
- java提高篇---LinkedList
- 简单算法——使用栈实现括号匹配检验
- 刘强东:死掉的创业公司,几乎都违背这 4 点最基本的经济常识
- H5是轻应用,也是一个流量中枢