31S-Section-Solution

# 31S-Section-Solution - CS106X Autumn 2010 Handout 31S...

This preview shows pages 1–2. Sign up to view the full content.

CS106X Handout 31S Autumn 2010 November 8 th , 2010 Section Solution Solution 1: Binary Tree Recursion a.) void IdentifyEndpoints(node *arbitraryNode, node *& front, node *& back) { front = back = arbitraryNode; if (front == NULL) return; while (front->left != NULL) { front = front->left; } while (back->right != NULL) { back = back->right; } } b.) node *FlattenTree(node *root) { if (root == NULL) return NULL; node *leftList = FlattenTree(root->left); node *rightList = FlattenTree(root->right); node *leftFront, *leftBack, *rightFront, *rightBack; IdentifyEndpoints(leftList, leftFront, leftBack); IdentifyEndpoints(rightList, rightFront, rightBack); root->left = leftBack; if (leftBack != NULL) leftBack->right = root; else leftFront = root; root->right = rightFront; if (rightFront != NULL) rightFront->left = root; return leftFront; } Solution 2: Tree Rotations a) void RotateLeft(node **parentp) { node *parent = *parentp; node *rightChild = parent->right; parent->right = rightChild->left; rightChild->left = parent;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/13/2011 for the course CS 106X taught by Professor Cain,g during the Fall '08 term at Stanford.

### Page1 / 3

31S-Section-Solution - CS106X Autumn 2010 Handout 31S...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online