Convert Binary Search Tree (BST) to Sorted Doubly-Linked List -- leap of faith with recursion.
摘要:1st Solution:When I first see this problem, my first thought was in-order traversal. Couldn’t we modify the nodes’ left and right pointers as we do an in-order traversal of the tree? However, we have to beware not to modify the pointers and accessing it at a later time.As we traverse the tree in-ord
阅读全文
posted @
2013-05-29 16:18
Alan Yang
阅读(530)
推荐(0)
Become A Better Developer By Indexing Your Brain
摘要:From: http://www.skorks.com/2009/09/become-a-better-developer-by-indexing-your-brain/ By Alan SkorkinThis is the fourth post in the teaching and learning series. The teaching and learning series includes the following posts:All Developers Should Know How They Learn BestThe Secret Of Being A Great M.
阅读全文
posted @
2013-05-27 18:37
Alan Yang
阅读(216)
推荐(0)
convert sorted array/list to a height balanced BST.
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST./*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/public class Solution {public TreeNode sortedArrayToBST(int[] num) {/
阅读全文
posted @
2013-05-21 17:52
Alan Yang
阅读(311)
推荐(0)
Two sum.
摘要:Given an array A[] and a number x, check for pair in A[] with sum as xhttp://www.geeksforgeeks.org/write-a-c-program-that-given-a-set-a-of-n-numbers-and-another-number-x-determines-whether-or-not-there-exist-two-elements-in-s-whose-sum-is-exactly-x/ public int[] twoSum(int[] numbers, int target) ...
阅读全文
posted @
2013-05-20 17:22
Alan Yang
阅读(225)
推荐(0)
Palindrome
摘要:Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have solved
阅读全文
posted @
2013-05-20 14:45
Alan Yang
阅读(172)
推荐(0)
Best Time to Buy and Sell Stock
摘要:Best Time to Buy and Sell StockSay you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.//Time complexity O(n), auxi
阅读全文
posted @
2013-05-17 16:22
Alan Yang
阅读(168)
推荐(0)
Maximum difference between two elements
摘要:Maximum difference between two elementsFrom http://www.geeksforgeeks.org/maximum-difference-between-two-elements/April 10, 2010Given an array arr[] of integers, find out the difference between any two elementssuch that larger element appears after the smaller number in arr[].Examples: If array is [2
阅读全文
posted @
2013-05-17 14:58
Alan Yang
阅读(293)
推荐(0)
Matrix issue
摘要:Search a 2D MatrixWrite an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following m
阅读全文
posted @
2013-05-17 00:18
Alan Yang
阅读(326)
推荐(0)