Binary Tree Tutorials
Binary Tree Tutorials
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.
e.g.
3
12L
13R
1
/ \
2 3
2
3 Question
• Inorder Travesal Problem Link
• Preorder Traversal 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