Use one queue + size variable

  1. public class Solution {
  2. public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
  3. ArrayList result = new ArrayList();
  4. if (root == null)
  5. return result;
  6. Queue<TreeNode> queue = new LinkedList<TreeNode>();
  7. queue.offer(root);
  8.  
  9. while (!queue.isEmpty()) {
  10. int size = queue.size();
  11. ArrayList<Integer> level = new ArrayList<Integer>();
  12. for (int i = 0; i < size; i++) {
  13. TreeNode head = queue.poll();
  14. level.add(head.val);
  15. if (head.left != null)
  16. queue.offer(head.left);
  17. if (head.right != null)
  18. queue.offer(head.right);
  19. }
  20. result.add(level);
  21. }
  22. return result;
  23. }
  24. }

Binary Search Tree BST Template的更多相关文章

  1. Lowest Common Ancestor of a Binary Search Tree (BST)

    Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node ...

  2. PAT 1099 Build A Binary Search Tree[BST性质]

    1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  3. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  4. Convert Sorted List to Balanced Binary Search Tree (BST)

    (http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...

  5. Convert Sorted Array to Balanced Binary Search Tree (BST)

    (http://leetcode.com/2010/11/convert-sorted-array-into-balanced.html) Given an array where elements ...

  6. Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

    (http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html) Convert a BST to a sorted circu ...

  7. Binary Search Tree DFS Template

    Two methods: 1. Traverse 2. Divide & Conquer // Traverse: usually do not have return value publi ...

  8. 【题解】【BST】【Leetcode】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

    题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 例如: 2,8 - ...

随机推荐

  1. WPF多线程问题

    最近碰到这种多线程问题都是在WPF项目中. 1. 问题是这样.有个一主界面线程,然后background线程启动,这个background线程试图去修改主界面里面的数据. 造成死锁. 调用过程,主界面 ...

  2. css中的段落样式及背景

    一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...

  3. Android开发(20)--RadioGroup的使用

    RadioGroup 有时候比較实用.主要特征是给用户提供多选一机制. MainActivity.java package com.example.lesson16_radio; import and ...

  4. hdu 3056 病毒侵袭持续中 AC自己主动机

    http://acm.hdu.edu.cn/showproblem.php?pid=3065 刘汝佳的模板真的非常好用,这道题直接过 学到: cnt数组记录单词出现次数 以及map存储单词编号与字符串 ...

  5. Sublime text3配置LiveReload

    Tip: LiveReload是很棒的插件,可以在浏览器中实时预览,但是在Sublime text3里,从Package Control中安装的LiveReload是无法使用的,但是可以选择手动安装解 ...

  6. SQL Server常用脚本

    一.迁移登录用户脚本: select 'create login [' + p.name + '] ' + case when p.type in('U','G') then 'from window ...

  7. oracle 数据库 if...elsif...语句

    CREATE OR REPLACE FUNCTION "UFN_GETIDS" (    OPEKIND   IN   VARCHAR2,-- 查询类型    PARAMS IN ...

  8. 怎么判断PC端浏览器内核

    browser = {             /**              * @property {boolean} ie 检测当前浏览器是否为IE              */       ...

  9. javascript作用域和作用域链

    1.作用域 作用域,它是指对某一变量和方法具有访问权限的代码空间.当我们在定义变量的时候,会定义两种变量,一种是在全局环境下定义的变量,叫全局变量,一种是在函数中定义的变量叫局部变量.全局变量的作用域 ...

  10. 为什么要用Math.sqrt(i)方法

    java 练习题 判断 101-200 之间有多少个素数,并输出所有素数 public class Prime { public static int count = 0; public static ...