摘要: Implement pow(x,n).divide and conquer method: the running time is O(ln n) 1 public class Solution{ 2 public double pow(double x, int n){ 3 double result = 0; 4 long temp = n; 5 if(temp >= 0) 6 result = power(x, temp); 7 else 8 result =... 阅读全文
posted @ 2014-01-29 23:38 Averill Zheng 阅读(136) 评论(0) 推荐(0) 编辑
摘要: 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?solution 1 using O(n) space: 1 /** 2 * Definition for binary tree 3 * public cla.. 阅读全文
posted @ 2014-01-29 13:44 Averill Zheng 阅读(137) 评论(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. 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val... 阅读全文
posted @ 2014-01-29 03:31 Averill Zheng 阅读(142) 评论(0) 推荐(0) 编辑
摘要: Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree. 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { va... 阅读全文
posted @ 2014-01-29 03:15 Averill Zheng 阅读(135) 评论(0) 推荐(0) 编辑