请问向国内转一两万美元的话怎么最划算?# Money - 海外理财
l*h
1 楼
搜索了一下,网上一种常见的解法如下:pathLen一直增加,不断把节点的值加进去。
我怎么觉得有错, 比如
1
/ \
2 3
\ /\
4 5 6
打印出来岂不是
1, 2, 4
1, 2, 4, 3, 5
1, 2, 4, 3, 6
了?
http://k2code.blogspot.com/2011/05/root-to-leaf-path.html
/*
Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work.
*/
void printPaths(struct node* node) {
int path[1000]; printPathsRecur(node, path, 0);
}
/*
Recursive helper function -- given a node, and an array containing
the path from the root node up to but not including this node,
print out all the root-leaf paths.
*/
void printPathsRecur(struct node* node, int path[], int pathLen) {
if (node==NULL) return;
// append this node to the path array
path[pathLen] = node->data;
pathLen++;
// it's a leaf, so print the path that led to here
if (node->left==NULL && node->right==NULL) {
printArray(path, pathLen);
}
else {
// otherwise try both subtrees
printPathsRecur(node->left, path, pathLen);
printPathsRecur(node->right, path, pathLen);
}
}
// Utility that prints out an array on a line.
void printArray(int ints[], int len) {
int i;
for (i=0; i
printf("%d ", ints[i]);
}
printf("\n");
}
我怎么觉得有错, 比如
1
/ \
2 3
\ /\
4 5 6
打印出来岂不是
1, 2, 4
1, 2, 4, 3, 5
1, 2, 4, 3, 6
了?
http://k2code.blogspot.com/2011/05/root-to-leaf-path.html
/*
Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work.
*/
void printPaths(struct node* node) {
int path[1000]; printPathsRecur(node, path, 0);
}
/*
Recursive helper function -- given a node, and an array containing
the path from the root node up to but not including this node,
print out all the root-leaf paths.
*/
void printPathsRecur(struct node* node, int path[], int pathLen) {
if (node==NULL) return;
// append this node to the path array
path[pathLen] = node->data;
pathLen++;
// it's a leaf, so print the path that led to here
if (node->left==NULL && node->right==NULL) {
printArray(path, pathLen);
}
else {
// otherwise try both subtrees
printPathsRecur(node->left, path, pathLen);
printPathsRecur(node->right, path, pathLen);
}
}
// Utility that prints out an array on a line.
void printArray(int ints[], int len) {
int i;
for (i=0; i
printf("%d ", ints[i]);
}
printf("\n");
}