Given a binary search tree, print right view of it, i.e all the nodes which will be seen if binary search tree is looked at from right hand side
http://www.geeksforgeeks.org/print-right-view-binary-tree-2/
Pre-order traversal, but right tree first then left tree, also keep track of maximum level so far.
http://www.geeksforgeeks.org/print-right-view-binary-tree-2/
The key is: Pre-Order Traverse, but right child first: Root -> Right -> Left
The Right view contains all nodes that are last nodes in their levels. A simple solution is to dolevel order traversal and print the last node in every level.
The Right view contains all nodes that are last nodes in their levels. A simple solution is to dolevel order traversal and print the last node in every level.
The problem can also be solved using simple recursive traversal. We can keep track of level of a node by passing a parameter to all recursive calls. The idea is to keep track of maximum level also. And traverse the tree in a manner that right subtree is visited before left subtree. Whenever we see a node whose level is more than maximum level so far, we print the node because this is the last node in its level (Note that we traverse the right subtree before left subtree).
void
rightViewUtil(
struct
Node *root,
int
level,
int
*max_level)
{
// Base Case
if
(root==NULL)
return
;
// If this is the last Node of its level
if
(*max_level < level)
{
printf
(
"%d\t"
, root->data);
*max_level = level;
}
// Recur for right subtree first, then left subtree
rightViewUtil(root->right, level+1, max_level);
rightViewUtil(root->left, level+1, max_level);
}
void
rightView(
struct
Node *root)
{
int
max_level = 0;
rightViewUtil(root, 1, &max_level);
}
https://gyaneshwarpardhi.wordpress.com/2014/06/22/print-right-view-of-a-binary-tree/
BFS, Level Order Traverse => Using two queues(Or use a count)
BFS, Level Order Traverse => Using two queues(Or use a count)
static
void
printRightViewOfBinaryTree(treeNode *rootNode) {
//queues to store current and next level nodes
queue<treeNode*> Q1;
queue<treeNode*> Q2;
Q1.push(rootNode);
while
(!Q1.empty()) {
treeNode * temp = Q1.front();
Q1.pop();
if
(temp->leftChild)
Q2.push(temp->leftChild);
if
(temp->rightChild)
Q2.push(temp->rightChild);
if
(Q1.empty()) {
//print last element of each level
cout << temp->val << endl;
//move nodes form next level to current level
swap(Q1, Q2);
}
}
}
Alternative solution: Use an queue to do level traversal and Print last value in each level.
The Right view contains all nodes that are last nodes in their levels. A simple solution is to do level order traversal and print the last node in every level.Pre-order traversal, but right tree first then left tree, also keep track of maximum level so far.
Read full article from Algorithms and Me: Binary Search Tree : Print right view of the treevoid print_right_view(Node * node, int curr_level, int *max_level){if(node == NULL) return;if(curr_level > *max_level){printf("%d ", node->value);*max_level = curr_level;}print_right_view(node->right, curr_level+1, max_level);print_right_view(node->left, curr_level+1, max_level);}