摘要: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?Sulotion: Considers a BST as a storted array, so we can traverse the BST inorderly 阅读全文
posted @ 2013-11-21 15:33 假日笛声 阅读(201) 评论(0) 推荐(0) 编辑
摘要: Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.Summary: DP, calucate the minimun sum from start point to every cell unitil we traverse ever 阅读全文
posted @ 2013-11-21 15:08 假日笛声 阅读(422) 评论(0) 推荐(0) 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Solution: Just do it recursively. 1 TreeNode *build(vector &preorder, int pstart, int pend, vector &inorder, int istart, int iend) { 2 TreeNode * root = ... 阅读全文
posted @ 2013-11-21 12:09 假日笛声 阅读(185) 评论(0) 推荐(0) 编辑