Binary Search Tree BST Template

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 }
时间: 2024-10-29 04:41:43

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* LCA(Node* root, Node* p, Node* q) { if (!root || !p || !q) return NULL; if (max(p->data, q->data) < root->data) return LCA(root->left, p,

【LeetCode】 Recover Binary Search Tree BST 中序遍历

题目:Recover Binary Search Tree <span style="font-size:18px;">/* * LeetCode: recover the binary search tree * 题目:二叉树中有两个节点被交换了位置,找出它们,并且将它们换回来,要求用o(n)的连续空间 * 知识点:1.BST树的特点:中序遍历后的节点的排列是按照非降的顺序 * 思路:按照特点中序遍历,当遇到逆序的节点则按照保存相关节点,注意分为,交换的两个点是否相邻的两

uva 1264 - Binary Search Tree(BST)

题目链接:uva 1264 - Binary Search Tree 题目大意:给定一个插入顺序,要求输出有多少种插入顺序,使得生成的BST一样. 解题思路:组合数学+BST的性质,起始左右两个子树的节点之间是没有影响的.所以逐层递推上去即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int max

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

UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以 代码: #include <cstdio> #include <cstring> typedef long long ll; const int MAXNODE =

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 where elements are sorted in ascending order, convert it to a height balanced BST. Code: BinaryTree* sortedListToBST(ListNode *& list, int start, int

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 circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Code: v

Binary Search Tree DFS Template

Two methods: 1. Traverse 2. Divide & Conquer 1 // Traverse: usually do not have return value 2 public class Solution { 3 public void traverse(TreeNode root) { 4 if (root == null) 5 return; 6 traverse(root.left); 7 traverse(root.right); 8 } 9 } 10 11

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 -->6 2,4-–>2 原文描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA

501. Find Mode in Binary Search Tree查找BST中的众数

[抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to t