You are browsing the archive for binary tree.

Lowest Common Ancestor of a Binary Tree Part II

July 21, 2011 in binary tree

Given a binary tree, find the lowest common ancestor of two given nodes in the tree. Each node contains a parent pointer which links to its parent.

Read the rest of this entry →

Lowest Common Ancestor of a Binary Tree Part I

July 18, 2011 in binary tree

Given a binary tree, find the lowest common ancestor of two given nodes in the tree.

Read the rest of this entry →

Lowest Common Ancestor of a Binary Search Tree (BST)

July 17, 2011 in binary tree

Given a binary search tree (BST), find the lowest common ancestor of two given nodes in the BST.

Read the rest of this entry →

Construct Binary Tree From Inorder and Preorder/Postorder Traversal

April 20, 2011 in binary tree

Given preorder and inorder traversal of a tree, construct the binary tree.

Read the rest of this entry →

Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

November 29, 2010 in binary tree, linked list

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.

Read the rest of this entry →

Convert Sorted List to Balanced Binary Search Tree (BST)

November 27, 2010 in binary tree, linked list

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

Read the rest of this entry →

Convert Sorted Array to Balanced Binary Search Tree (BST)

November 25, 2010 in binary tree, divide and conquer

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

Read the rest of this entry →

Largest Binary Search Tree (BST) in a Binary Tree

November 22, 2010 in binary tree

Given a binary tree, find the largest Binary Search Tree (BST), where largest means BST with largest number of nodes in it. The largest BST may or may not include all of its descendants.

Read the rest of this entry →

Largest Subtree Which is a Binary Search Tree (BST)

November 18, 2010 in binary tree

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.

Read the rest of this entry →

Binary Tree Post-Order Traversal Iterative Solution

October 27, 2010 in binary tree

Given a binary tree, print the elements in post-order iteratively without using recursion.

Read the rest of this entry →

Print Edge Nodes (Boundary) of a Binary Tree

October 20, 2010 in binary tree

Print all edge nodes of a complete binary tree anti-clockwise.
That is all the left most nodes starting at root, then the leaves left to right and finally all the rightmost nodes.
In other words, print the boundary of the tree.

Variant: Print the same for a tree that is not complete.

Read the rest of this entry →

Serialization/Deserialization of a Binary Tree

September 29, 2010 in binary tree

Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called ‘serialization’ and reading back from the file to reconstruct the exact same binary tree is ‘deserialization’.

Read the rest of this entry →

Saving a Binary Search Tree to a File

September 28, 2010 in binary tree

Describe an algorithm to save a Binary Search Tree (BST) to a file in terms of run-time and disk space complexity. You must be able to restore to the exact original BST using the saved format.

Read the rest of this entry →

How to Pretty Print a Binary Tree

September 18, 2010 in binary tree

Have you ever thought of pretty-printing a binary tree?
Read the rest of this entry →

Printing a Binary Tree in Zig Zag Level-Order

September 18, 2010 in binary tree

Given a binary tree, print out the tree in zig zag level order (ie, from left to right, then right to left for the next level and alternate between). Output a newline after the end of each level. Read the rest of this entry →

Binary Tree Level-Order Traversal Using Depth First Search (DFS)

September 17, 2010 in binary tree

Given a binary tree, print out the tree in level order (ie, from left to right, level by level). Output a newline after the end of each level. Breadth First Search (BFS) is not allowed. Read the rest of this entry →

Printing a Binary Tree in Level Order

September 16, 2010 in binary tree

Given a binary tree, print out the tree in level order (ie, from left to right, level by level). Output a newline after the end of each level. Read the rest of this entry →

Determine if a Binary Tree is a Binary Search Tree (BST)

September 14, 2010 in binary tree

Write a function isBST(BinaryTree *node) to verify if a given binary tree is a Binary Search Tree (BST) or not.

Read the rest of this entry →

Maximum Height (Depth) of a Binary Tree

April 21, 2010 in binary tree

Given a binary tree, find its maximum height.

Read the rest of this entry →

A binary tree problem – Populating next right pointers in each node

March 12, 2010 in binary tree

Given a binary tree

Populate the nextRight pointers in each node.

Read the rest of this entry →