Binary Search Tree BST Template
Use one queue + size variable
- public class Solution {
- public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
- ArrayList result = new ArrayList();
- if (root == null)
- return result;
- Queue<TreeNode> queue = new LinkedList<TreeNode>();
- queue.offer(root);
- while (!queue.isEmpty()) {
- int size = queue.size();
- ArrayList<Integer> level = new ArrayList<Integer>();
- for (int i = 0; i < size; i++) {
- TreeNode head = queue.poll();
- level.add(head.val);
- if (head.left != null)
- queue.offer(head.left);
- if (head.right != null)
- queue.offer(head.right);
- }
- result.add(level);
- }
- return result;
- }
- }
Binary Search Tree BST Template的更多相关文章
- 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 ...
- 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 ...
- UVA 1264 - Binary Search Tree(BST+计数)
UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...
- 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 ...
- 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 ...
- 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 ...
- Binary Search Tree DFS Template
Two methods: 1. Traverse 2. Divide & Conquer // Traverse: usually do not have return value publi ...
- 【题解】【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 ...
- 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 - ...
随机推荐
- WPF多线程问题
最近碰到这种多线程问题都是在WPF项目中. 1. 问题是这样.有个一主界面线程,然后background线程启动,这个background线程试图去修改主界面里面的数据. 造成死锁. 调用过程,主界面 ...
- css中的段落样式及背景
一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...
- Android开发(20)--RadioGroup的使用
RadioGroup 有时候比較实用.主要特征是给用户提供多选一机制. MainActivity.java package com.example.lesson16_radio; import and ...
- hdu 3056 病毒侵袭持续中 AC自己主动机
http://acm.hdu.edu.cn/showproblem.php?pid=3065 刘汝佳的模板真的非常好用,这道题直接过 学到: cnt数组记录单词出现次数 以及map存储单词编号与字符串 ...
- Sublime text3配置LiveReload
Tip: LiveReload是很棒的插件,可以在浏览器中实时预览,但是在Sublime text3里,从Package Control中安装的LiveReload是无法使用的,但是可以选择手动安装解 ...
- SQL Server常用脚本
一.迁移登录用户脚本: select 'create login [' + p.name + '] ' + case when p.type in('U','G') then 'from window ...
- oracle 数据库 if...elsif...语句
CREATE OR REPLACE FUNCTION "UFN_GETIDS" ( OPEKIND IN VARCHAR2,-- 查询类型 PARAMS IN ...
- 怎么判断PC端浏览器内核
browser = { /** * @property {boolean} ie 检测当前浏览器是否为IE */ ...
- javascript作用域和作用域链
1.作用域 作用域,它是指对某一变量和方法具有访问权限的代码空间.当我们在定义变量的时候,会定义两种变量,一种是在全局环境下定义的变量,叫全局变量,一种是在函数中定义的变量叫局部变量.全局变量的作用域 ...
- 为什么要用Math.sqrt(i)方法
java 练习题 判断 101-200 之间有多少个素数,并输出所有素数 public class Prime { public static int count = 0; public static ...