2014年3月18日

Lowest Common Ancestor of a Binary Tree

摘要: Given a binary tree, find the lowest common ancestor of two given nodes in the tree. 1 // This function returns pointer to LCA of two given values n1 and n2. 2 // This function assumes that n1 and n2 are present in Binary Tree 3 struct Node *findLCA(struct Node* root, int n1, int n2) 4 { 5 // Ba... 阅读全文

posted @ 2014-03-18 08:55 longhorn 阅读(172) 评论(0) 推荐(0) 编辑

Shuffle Array

摘要: 写一个shuffle数组的算法给一个数组A[],里面的数是无序的,比如 A[]={1,2,3,7,3,2,5}shuffle完之后,使之满足A[0]=A[2]= a[2] ...., 当i为n就结束了当i为0, 2, 4...,前i - 1个元素已经满足了。 如果a[ i ] > a[i + 1] 交换a[ i ] 和 a[i + 1], 这样使得前i个都满足。当i为1,3,5... 同理, 如果a[ i ] A[i+1]) {10 swap(A, i, i+1);11 }12 }13 ... 阅读全文

posted @ 2014-03-18 06:44 longhorn 阅读(429) 评论(0) 推荐(0) 编辑

Lowest Common Ancestor of a Binary Search Tree

摘要: Given a binary search tree (BST), find the lowest common ancestor of two given nodes in the BST.4种情况。Both nodes are to the left of the tree.Both nodes are to the right of the tree.One node is on the left while the other is on the right.When the current node equals to either of the two nodes, this no 阅读全文

posted @ 2014-03-18 04:12 longhorn 阅读(144) 评论(0) 推荐(0) 编辑

Josephus problem

摘要: http://www.geeksforgeeks.org/josephus-problem-set-1-a-on-solution/ 1 int josephus(int n, int k) 2 { 3 if (n == 1) 4 return 1; 5 else 6 /* The position returned by josephus(n - 1, k) is adjusted because the 7 recursive call josephus(n - 1, k) considers the original position 8 ... 阅读全文

posted @ 2014-03-18 03:27 longhorn 阅读(614) 评论(0) 推荐(0) 编辑

导航