题目都是来自standford cs library:http://cslibrary.stanford.edu/110/BinaryTrees.html
是一个关于二叉树的专题,上面还有很多其他的专题,适合刚入门或者要找工作同学复习用。下面是我自己从二叉树这个专题当中选取的一些有代表性的题目。
基本数据结构如下:
struct node { int data; struct node* left; struct node* right; }
// suppose the variable "root" points to the tree
root = change(root);
We take the value returned by change(), and use it as the new value for root. This construct is a little awkward, but it avoids using reference parameters which confuse some C and C++ programmers, and Java does not have reference parameters at all. This allows us to focus on the recursion instead of the pointer mechanics. (For lots of problems that use reference parameters, see CSLibrary #105, Linked List Problems, http://cslibrary.stanford.edu/105/).
2
/ \
1 10
returns the tree...
2
/ \
1 10
/
5
The solution shown here introduces a newNode() helper function that builds a single node. The base-case/recursion structure is similar to the structure in lookup() -- each call checks for the NULL case, looks at the node at hand, and then recurs down the left or right subtree if needed.
/* Helper function that allocates a new node with the given data and NULL left and right pointers. */ struct node* NewNode(int data) { struct node* node = new(struct node); // "new" is like "malloc" node->data = data; node->left = NULL; node->right = NULL; return(node); } /* Give a binary search tree and a number, inserts a new node with the given number in the correct place in the tree. Returns the new root pointer which the caller should then use (the standard trick to avoid using reference parameters). */ struct node* insert(struct node* node, int data) { // 1. If the tree is empty, return a new, single node if (node == NULL) { return(newNode(data)); } else { // 2. Otherwise, recur down the tree if (data <= node->data) node->left = insert(node->left, data); else node->right = insert(node->right, data); return(node); // return the (unchanged) node pointer } }
int size(struct node* node) {}
/* Compute the number of nodes in a tree. */ int size(struct node* node) { if (node==NULL) { return(0); } else { return(size(node->left) + 1 + size(node->right)); } }
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
Root-to-leaf paths:
path 1: 5 4 11 7
path 2: 5 4 11 2
path 3: 5 8 13
path 4: 5 8 4 1
For this problem, we will be concerned with the sum of the values of such a path -- for example, the sum of the values on the 5-4-11-7 path is 5 + 4 + 11 + 7 = 27.
Given a binary tree and a sum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Return false if no such path can be found. (Thanks to Owen Astrachan for suggesting this problem.)
int hasPathSum(struct node* node, int sum)
/* Given a tree and a sum, return true if there is a path from the root down to a leaf, such that adding up all the values along the path equals the given sum. Strategy: subtract the node value from the sum when recurring down, and check to see if the sum is 0 when you run out of tree. */ int hasPathSum(struct node* node, int sum) { // return true if we run out of tree and sum==0 if (node == NULL) { return(sum == 0); } else { // otherwise check both subtrees int subSum = sum - node->data; return(hasPathSum(node->left, subSum) || hasPathSum(node->right, subSum)); } }
Given a binary tree, print out all of its root-to-leaf paths, one per line.
void printPaths(struct node* node)
/* 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<len; i++) { printf("%d ", ints[i]); } printf("\n"); }
So the tree...
4
/ \
2 5
/ \
1 3
is changed to...
4
/ \
5 2
/ \
3 1
The solution is short, but very recursive. As it happens, this can be accomplished without changing the root node pointer, so the return-the-new-root construct is not necessary. Alternately, if you do not want to change the tree nodes, you may construct and return a new mirror tree based on the original tree.
void mirror(struct node* node)
/* Change a tree so that the roles of the left and right pointers are swapped at every node. So the tree... 4 / \ 2 5 / \ 1 3 is changed to... 4 / \ 5 2 / \ 3 1 */ void mirror(struct node* node) { if (node==NULL) { return; } else { struct node* temp; // do the subtrees mirror(node->left); mirror(node->right); // swap the pointers in this node temp = node->left; node->left = node->right; node->right = temp; } }
int sameTree(struct node* a, struct node* b)
/* Given two trees, return true if they are structurally identical. */ int sameTree(struct node* a, struct node* b) { // 1. both empty -> true if (a==NULL && b==NULL) return(true); // 2. both non-empty -> compare them else if (a!=NULL && b!=NULL) { return( a->data == b->data && sameTree(a->left, b->left) && sameTree(a->right, b->right) ); } // 3. one empty, one not -> false else return(false); }