Skip to content

Instantly share code, notes, and snippets.

View charlespunk's full-sized avatar

charlespunk charlespunk

View GitHub Profile
Implement a function to check if a binary tree is balanced.
For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.
Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
Given a sorted(increasing order) array, write an algorithm to create a binary search tree with minimal height.
Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth(e.g., if you have a tree with depth D, you'll have D linked lists).
Implement a function to check is a binary tree is a binary search tree.
Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in a binary search tree. You may assume that each node has a link to its parent.
Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid sorting additional nodes in a data structure. NODE: This is not necessarily a binary search tree.
You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1.
A tree T2 is a subtree if T1 if there exists a node n in T1 such that the subtree of n is identical to T2. That is, if you cut of the tree at node n, the two trees would be identical.
/*
CacheForRecentlyAccess
[A, B, C, D]
[B, C, D, A]
[C, D, A, F, G]
[C, A, F, G, D]
[A, F, G, D, N] C evicted
[F, G, D, N, M] A evicted
You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum to a given vaue. Note that a path can start or end anythere in the tree.