二叉树
nicaishibiantai
这个作者很懒,什么都没留下…
展开
-
二叉树-recover binary search tree
http://leetcode.com/onlinejudge#question_99两个node被调换了,回复原来的二叉树。常数空间。做这道题的时候,一个血的教训就是在递归的时候,不要global初始class member。开始我没有设 prev =null; first =null; second =null;而是仅仅声明了原创 2014-08-11 11:31:03 · 297 阅读 · 0 评论 -
二叉树-Convert Sorted List to Binary Search Tree
这道题不是很好懂,先把跑过的答案贴在这儿public class Solution { public TreeNodesortedListToBST(ListNode head) { // Start typing your Java solution below // DO NOT write main() function if (原创 2014-08-11 11:31:00 · 359 阅读 · 0 评论 -
Lowest common ancestor
This code is only valid if A and B exist in the tree public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) { if (root == null) { return null; }原创 2015-02-05 13:27:46 · 1034 阅读 · 0 评论 -
Lintcode - Serialization and Deserialization Of Binary Tree
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary原创 2015-02-09 09:09:26 · 1329 阅读 · 0 评论 -
Lintcode - Remove Node in Binary Search Tree
Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should kee原创 2015-02-09 11:33:08 · 1808 阅读 · 0 评论 -
二叉树-循环遍历
前序遍历的基本思想就是visit 当前node,push rightnode,当前node向左下移动。如果当前node是空,那么从stack弹出一个node,并把它当做一个崭新的rootnode,继续循环。preOrder (Node node) { Stack s = newStack(); while (node != null ||!s.isEmpty()) {i原创 2014-08-11 11:30:47 · 471 阅读 · 0 评论 -
Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()原创 2015-01-04 14:30:52 · 387 阅读 · 0 评论 -
Save BST to a file
http://articles.leetcode.com/2010/09/saving-binary-search-tree-to-file.htmlpreorder traversal将bst存下来。如何恢复?依然preorder。母节点的数值决定了子节点可以存储的范围,所以我们拿到一个新的数字时,看他是不是在母节点的规定范围内;否则就继续到母节点的另外一个子树中尝试。p原创 2015-04-12 02:26:38 · 380 阅读 · 0 评论 -
Pretty print binary tree
http://articles.leetcode.com/2010/09/how-to-pretty-print-binary-tree.html简化版本的C++const int box_size = 3;std::queue q;q.push(m_root);size_t current_level_nodes = 1;size_t next_current_level_n原创 2015-04-12 03:28:24 · 680 阅读 · 0 评论 -
Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1原创 2015-04-26 08:22:27 · 398 阅读 · 0 评论 -
Lintcode - Segment Tree Build
The structure of Segment Tree is a binary tree which each node has two attributes start andend denote an segment / interval.start and end are both integers, they should be assigned in following原创 2015-05-03 07:13:22 · 1482 阅读 · 0 评论 -
Lintcode - Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding SegmentTree, each node stores an extra attributemax to denote the maximum number in the interval o原创 2015-05-03 07:17:19 · 1637 阅读 · 0 评论 -
二叉树-populate next node
// populating next node when tree is balancedpublic class Solution { public voidconnect(TreeLinkNode root) { TreeLinkNode leftNode = root; while (leftNode != null) {原创 2014-08-11 11:30:56 · 349 阅读 · 0 评论 -
Unique binary search trees
I. Given n,how many structurally unique BST's (binarysearch trees) that store values 1...n?For example,Given n =3, there are a total of 5 unique BST's. 1 3 3 2 1原创 2014-08-11 11:31:37 · 326 阅读 · 0 评论 -
二叉树-preorder+inorder 与& postorder+inorder
拿preorder+inorder为例。1. preorder的第一个数必然是根节点2. 在inorder数组中查找这个数,得到rootIndex3. rootIndex左边的数组即为left child node,右边的数组right node4. 左右数组分别递归。// Construct Binary Tree from Preorder and InorderTraversa原创 2014-08-11 11:30:54 · 2095 阅读 · 0 评论 -
二叉树-Lowest Common Ancestor
1. Binary search tree:只需要判断当前node值是否在两个node之间2. Binary tree with parent link:先求两个node的height, 然后向上traverse3. Binary tree without parent link:solution 1: top-bottom int countMatches(TreeNode原创 2014-08-22 12:31:04 · 398 阅读 · 0 评论 -
二叉树-serialization deserialization
serialization public void serialization (List result, TreeNode node) { if (node == null) { result.add("#"); return; } result.add(String.valueOf(node.val)); serialization(result, node原创 2014-08-24 01:52:19 · 501 阅读 · 0 评论 -
二叉树-flatten binary search tree to linked lis
采用preorder结构。根节点首先输出,下一个节点为左子节点,左子节点的末端链接根节点的右子节点。// Flatten Binary Tree to Linked Listclass Solution { //循环 public voidflatten(TreeNode root) { if (root == null) { retur原创 2014-08-11 11:30:58 · 378 阅读 · 0 评论 -
二叉树求大和最小depth
后序遍历的变形题是maxdepth、minDepth.做法为当运行到最后一个else是,我们已经知道或者为最下子节点或者为有下节点,不论那种情况,计算max(maxDepth,s.size()),总会得到最大depthpublic class Solution { public intmaxDepth(TreeNode root) { // Start typin原创 2014-08-11 11:30:49 · 356 阅读 · 0 评论 -
二叉树-Largest BST in a tree
http://leetcode.com/2010/11/largest-binary-search-tree-bst-in.htmlAll deschttp://leetcode.com/2010/11/largest-binary-search-tree-bst-in_22.html原创 2014-08-24 05:57:56 · 387 阅读 · 0 评论 -
二叉树-Binary tree maximum path sum
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return 6.原创 2014-08-24 12:39:52 · 386 阅读 · 0 评论 -
二叉树-对称树
先上自己开始最图森破的解法,其实就是自己serialize左右二叉树,然后对比arraylist: public booleanisSymmetric(TreeNode root) { // Start typing your Java solution below // DO NOT write main() function if (原创 2014-08-11 11:30:52 · 377 阅读 · 0 评论 -
开始研究二叉树
两个指针告一段落,接下来二叉树走起!mit上总结有12种traversal法,可惜我只会recursive的写法。目前应该下手的点是:1. iterative traversal全部写一遍2. 学习iterator附原贴:http://www.mitbbs.com/article_t1/JobHunting/32504905_0_1.html1. pre-order recursive原创 2014-08-11 11:30:45 · 408 阅读 · 0 评论 -
二叉树-Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal My SubmissionsGiven a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for th原创 2014-08-17 13:36:01 · 360 阅读 · 0 评论 -
Lintcode - Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in this node's interval.Implement a modify function with three parameter root, index and value to原创 2015-05-03 07:39:22 · 1073 阅读 · 0 评论