-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
klb3713
committed
Jul 14, 2014
1 parent
1673285
commit 978f651
Showing
22 changed files
with
1,906 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <iostream> | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
#define MAX 10001 | ||
|
||
bool VerifySquenceOfBST(unsigned long *arr, int n) | ||
{ | ||
int i; | ||
if (arr == NULL || n <= 0) | ||
return false; | ||
unsigned long int root = arr[n - 1]; | ||
for (i = 0;i < n - 1; i++) | ||
{ | ||
if (arr[i] > root) | ||
break; | ||
} | ||
int j; | ||
for (j = i; j < n - 1; j ++) | ||
{ | ||
if (arr[j] < root) | ||
return false; | ||
} | ||
//left child tree | ||
bool left = true;; | ||
if (i > 0) | ||
left = VerifySquenceOfBST(arr, i); | ||
//right child tree | ||
bool right = true; | ||
if (i < n - 1) | ||
right = VerifySquenceOfBST(arr + i, n - i - 1); | ||
return (left && right); | ||
} | ||
|
||
int main(void) | ||
{ | ||
int n; | ||
unsigned long arr[MAX]; | ||
while (cin >> n) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
scanf("%lu", &arr[i]); | ||
if (VerifySquenceOfBST(arr, n)) | ||
printf("Yes\n"); | ||
else | ||
printf("No\n"); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include <iostream> | ||
#include <cstdio> | ||
#include <vector> | ||
#include <stack> | ||
using namespace std; | ||
|
||
#define MAX 10001 | ||
|
||
struct BinaryTreeNode{ | ||
int value; | ||
int NO; | ||
BinaryTreeNode *left; | ||
BinaryTreeNode *right; | ||
}; | ||
|
||
void CreateTree(BinaryTreeNode ** root, int n) | ||
{ | ||
BinaryTreeNode *index; | ||
int tmp, tmp1, tmp2; | ||
vector<BinaryTreeNode *> node_arr; | ||
if ((*root) != NULL || n <= 0 ) | ||
return ; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
index = new BinaryTreeNode; | ||
index->left = NULL; | ||
index->right = NULL; | ||
index->value = 0; | ||
index->NO = i + 1; | ||
node_arr.push_back(index); | ||
} | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> tmp; | ||
index = node_arr[i]; | ||
index->value = tmp; | ||
cin >> tmp1 >> tmp2; | ||
if (tmp1 > tmp2) | ||
{ | ||
tmp = tmp1; | ||
tmp1 = tmp2; | ||
tmp2 = tmp; | ||
} | ||
if (tmp1 != -1) | ||
index->left = node_arr[tmp1 - 1]; | ||
if (tmp2 != -1) | ||
index->right = node_arr[tmp2 - 1]; | ||
} | ||
*root = node_arr[0]; | ||
return ; | ||
} | ||
|
||
|
||
|
||
void DeleteTree(BinaryTreeNode **root) | ||
{ | ||
BinaryTreeNode *index; | ||
index = *root; | ||
if ((*root) == NULL) | ||
return ; | ||
if (index->left) | ||
DeleteTree(&(index->left)); | ||
if (index->right) | ||
DeleteTree(&(index->right)); | ||
delete index; | ||
index = NULL; | ||
} | ||
|
||
void FindPathPartition(BinaryTreeNode *root, int expected_sum, int ¤t_sum, vector<BinaryTreeNode *> &path) | ||
{ | ||
// calculate the current_sum, and push this node into vector. | ||
// we use vector to simulate a stack, because it can print out the path. | ||
current_sum += root->value; | ||
path.push_back(root); | ||
BinaryTreeNode *index; | ||
// if this is a leaf node, and expected_sum is equal to current_sum, print the path | ||
bool is_leaf = (root->left == NULL) && (root->right == NULL); | ||
if (current_sum == expected_sum && is_leaf) | ||
{ | ||
printf("A path is found: "); | ||
vector<BinaryTreeNode *>::iterator iter = path.begin(); | ||
for (; iter != path.end(); iter++) | ||
{ | ||
index = *iter; | ||
printf("%d", index->NO); | ||
if (iter != path.end() - 1) | ||
printf(" "); | ||
else | ||
printf("\n"); | ||
} | ||
} | ||
// it is not a leaf node, see its children | ||
if (root->left) | ||
FindPathPartition(root->left, expected_sum, current_sum, path); | ||
if (root->right) | ||
FindPathPartition(root->right, expected_sum, current_sum, path); | ||
// before return to its father, delete itself from vector and | ||
// minus it value from current_sum | ||
current_sum -= root->value; | ||
path.pop_back(); | ||
|
||
} | ||
|
||
void FindPath(BinaryTreeNode *root, int expected_sum) | ||
{ | ||
if (root == NULL) | ||
return ; | ||
vector<BinaryTreeNode *> path; | ||
int current_sum = 0; | ||
FindPathPartition(root, expected_sum, current_sum, path); | ||
} | ||
|
||
int main(void) | ||
{ | ||
int n, k; | ||
freopen("in.txt", "r", stdin); | ||
freopen("out.txt", "w", stdout); | ||
while (cin >> n) | ||
{ | ||
BinaryTreeNode *root = NULL; | ||
cin >> k; | ||
CreateTree(&root, n); | ||
printf("result:\n"); | ||
FindPath(root, k); | ||
DeleteTree(&root); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include <iostream> | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
#define MAX 1001 | ||
|
||
struct ComplexListNode | ||
{ | ||
int value; | ||
ComplexListNode *next; | ||
ComplexListNode *sibling; | ||
}; | ||
|
||
void Create(int n, ComplexListNode **head) | ||
{ | ||
ComplexListNode* arr[MAX]; | ||
int i, tmp; | ||
if (n <= 0) | ||
{ | ||
*head = NULL; | ||
return ; | ||
} | ||
for (i = 0; i < n; i++) | ||
{ | ||
arr[i] = new ComplexListNode; | ||
arr[i]->next = NULL; | ||
arr[i]->sibling = NULL; | ||
arr[i]->value = 0; | ||
} | ||
for (i = 0; i < n; i++) | ||
{ | ||
scanf("%d", &tmp); | ||
arr[i]->value = tmp; | ||
if (i != n - 1) | ||
arr[i]->next = arr[i + 1]; | ||
} | ||
for (i = 0; i < n; i++) | ||
{ | ||
scanf("%d", &tmp); | ||
if (tmp != 0) | ||
arr[i]->sibling = arr[tmp - 1]; | ||
} | ||
*head = arr[0]; | ||
} | ||
|
||
|
||
void CloneNodes(ComplexListNode *head) | ||
{ | ||
ComplexListNode *index, *clone_index; | ||
index = head; | ||
while (index) | ||
{ | ||
clone_index = new ComplexListNode; | ||
clone_index->value = index->value; | ||
clone_index->next = index->next; | ||
index->next = clone_index; | ||
clone_index->sibling = NULL; | ||
index = clone_index->next; | ||
} | ||
} | ||
|
||
|
||
void ConnectSiblingNodes(ComplexListNode *head) | ||
{ | ||
ComplexListNode *index = head; | ||
while (index) | ||
{ | ||
if (index->sibling) | ||
index->next->sibling = index->sibling->next; | ||
index = index->next->next; | ||
} | ||
} | ||
|
||
ComplexListNode *ReconnectNodes(ComplexListNode *head) | ||
{ | ||
ComplexListNode *clone_head; | ||
ComplexListNode *index = head, *clone_index; | ||
if (head == NULL) | ||
return NULL; | ||
clone_head = index->next; | ||
index->next = clone_head->next; | ||
index = index->next; | ||
clone_index = clone_head; | ||
while (index) | ||
{ | ||
clone_index->next = index->next; | ||
clone_index = clone_index->next; | ||
index->next = clone_index->next; | ||
index = index->next; | ||
} | ||
return clone_head; | ||
} | ||
|
||
|
||
|
||
ComplexListNode* Clone(ComplexListNode *head) | ||
{ | ||
CloneNodes(head); | ||
ConnectSiblingNodes(head); | ||
return ReconnectNodes(head); | ||
} | ||
|
||
|
||
void PrintSibling(ComplexListNode *head) | ||
{ | ||
ComplexListNode *index = head; | ||
if (index == NULL) | ||
return ; | ||
while (index) | ||
{ | ||
if (index->sibling) | ||
printf("%d %d\n", index->value, index->sibling->value); | ||
else | ||
printf("%d 0\n", index->value); | ||
index = index->next; | ||
} | ||
} | ||
|
||
|
||
int main(void) | ||
{ | ||
int n; | ||
|
||
while (cin >> n) | ||
{ | ||
ComplexListNode *head, *clone_head; | ||
Create(n, &head); | ||
clone_head = Clone(head); | ||
PrintSibling(clone_head); | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.