Wednesday, May 11, 2011

Given a BST, create a mirror of it.

Problem Change a tree so that the roles of the left and right pointers are swapped at every node.  Example So the tree...        4       / \      2   5     / \    1   3 is changed to...        4       / \      5   2         / \        3   1 The solution is short,...

Given Binary Tree and sum, give root to leaf path such that all nodes add to that sum

We'll define a "root-to-leaf path" to be a sequence of nodes in a tree starting with the root node and proceeding downward to a leaf (a node with no children). We'll say that an empty tree contains no root-to-leaf paths. So for example, the following tree has exactly four root-to-leaf paths: 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   ...

Print binary search tree in increasing order

This updated information has been further expanded upon on my new website. You can find the updated details here: https://k5kc.com/cs/algorithms/print-binary-search-tree-in-increasing-order/. Given a binary search tree (aka an "ordered binary tree"), iterate over the nodes to print them out in increasing order. So the tree...        4      / \     2   5    / \   1   3Produces the output "1 2 3 4 5". This is known as an...

Find the minimum data value find in Binary Search tree (using cpp)

Given a non-empty binary search tree (an ordered binary tree), return the minimum data value found in that tree. Note that it is not necessary to search the entire tree. A maxValue() function is structurally very similar to this function. This can be solved with recursion or with a simple while loop. int minValue(struct node* node) { /* Given a non-empty binary search tree, return the minimum data value found in that tree. Note that the entire tree does not need to be searched.*/int minValue(struct node* node) { struct node* current = node; ...

Compute Maximum Depth or Height of binary search tree

Given a binary tree, compute its "maxDepth" -- the number of nodes along the longest path from the root node down to the farthest leaf node. The maxDepth of the empty tree is 0, the maxDepth of the tree below is 3.       1    /    \   2      3  /   \   /    \ 4   5  6   7 Recursive approach /* Compute the "maxDepth" of a tree -- the number of nodes along the longest path from the root node down to the farthest...

Getting the size of Binary Search Tree

This problem demonstrates simple binary tree traversal. Given a binary tree, count the number of nodes in the tree. Here is the code in c/cpp: /* 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)); } }...

Create Copy of a tree (cpp)

Given a binary tree,create the copy of the tree. node *copy(node* root) node *copy(node *root) node *temp; if(root==NULL)return(NULL); temp = (node *) malloc(sizeof(node));//or temp = newNode(root->data); temp->value = root->value; temp->left = copy(root->left); temp->right = copy(root->right); return(temp);...