0% found this document useful (0 votes)
51 views

Binary Tree Tutorials

The document discusses binary search trees and provides code for declaring tree nodes, performing inorder and postorder traversals, and lists tasks for the day including calculating subtree sizes, tree augmentation, modifying level order traversal, and taking a binary tree as input. It also lists several binary tree problems and their links for further reference.

Uploaded by

Avil Aneja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Binary Tree Tutorials

The document discusses binary search trees and provides code for declaring tree nodes, performing inorder and postorder traversals, and lists tasks for the day including calculating subtree sizes, tree augmentation, modifying level order traversal, and taking a binary tree as input. It also lists several binary tree problems and their links for further reference.

Uploaded by

Avil Aneja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Binary Search Tree

Ameya Zope, Aditya Bansal


March 20, 2019

1 Some Useful Stuff


1.1 Tree Node Declaration

s t r u c t Node{
i n t data ;
Node∗ l e f t ;
Node∗ r i g h t ;
Node ( i n t x ) {
data = x ;
l e f t = NULL;
r i g h t = NULL;
}
};
1.2 Inorder Traversal

v o i d i n o r d e r ( Node∗ r o o t ) {
i f ( r o o t==NULL) r e t u r n ;
else {
i n o r d e r ( r o o t −> l e f t ) ;
// perform some a c t i o n e . g . p r i n t t h e node ’ s v a l u e
i n o r d e r ( r o o t −>r i g h t ) ;
}
}
1.3 Postorder Traversal

v o i d p o s t o r d e r ( Node∗ r o o t ) {
i f ( r o o t==NULL) r e t u r n ;
else {
p o s t o r d e r ( r o o t −> l e f t ) ;
p o s t o r d e r ( r o o t −>r i g h t ) ;
// perform some a c t i o n e . g . p r i n t t h e node ’ s v a l u e
}
}

1
2 Today’s Tasks
2.1 Size of all Subtrees
Given a binary tree T, find the size of the subtree rooted at each node of the
tree.

2.2 Tree Augmentation


Given a rooted tree with n vertices indexed from 1 to n. Each vertex v is either
labelled 0 or 1. For each vertex v ,you find the absolute difference between no
of vertices labelled 0 and no of vertices labelled 1 in it’s subtree (vertex v is
included in the subtree of v).
Find the maximum of these differences. Tree is rooted at vertex 1.

2.3 Modifications to level Order


Print the diagonal traversal of a given binary tree.

2.4 Take a binary tree as input


The first line of the input denotes n, the number of nodes in the tree. Each of
the next n-1 lines are of the format: a b L or a b R
a b L : means node with index b is the left child of node with index a
a b R : means node with index c is the right child of node with index a

Assume that node 1 is always the root node.

e.g.
3
12L
13R

1
/ \
2 3

2
3 Question
• Inorder Travesal Problem Link
• Preorder Traversal Problem Link

• Postorder Traversal Problem Link


• Find nth Node in inorder traversal using only O(1) memory.
• Level order Traversal Problem Link
• Top View of a Tree Prolem Link

• Height of Binary Tree Problem Link


• Construct Tree from given Inorder and Preorder traversals Problem Link
• Construct a tree from Inorder and Level order traversals Problem Link

• Construct a complete binary tree from given array in level order fashion
Problem Link
• Party Problem Link
• Mirror Image Problem Link

• Given the root node a binary tree, find the maximal left spine of the tree.
The left spine is the simple path from the root that consists of only the
leftmost edges at each level

You might also like