Created
March 11, 2015 06:51
-
-
Save ravichandrae/ac43faf14a289deb25d1 to your computer and use it in GitHub Desktop.
Given a binary tree, how do we print the left view of it?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <queue> | |
using namespace std; | |
//Binary tree node type | |
struct TreeNode | |
{ | |
int val; | |
TreeNode *left, *right; | |
TreeNode(int v):val(v),left(NULL), right(NULL) | |
{ | |
} | |
}; | |
//Binary search tree insert procedure for creating binary tree to test | |
TreeNode* insert(TreeNode * root, int data) | |
{ | |
if( root == NULL ) | |
{ | |
root = new TreeNode(data); | |
return root; | |
} | |
if( data < root->val ) | |
root->left = insert( root->left, data ); | |
else | |
root->right = insert( root->right, data ); | |
return root; | |
} | |
//Use level order traversing to find out the first node in each level | |
//nodes at a particular level in the queue are separated by NULL node | |
void printLeftView(TreeNode *root) | |
{ | |
if( root == NULL ) | |
return; | |
queue<TreeNode*> nodeQ; | |
nodeQ.push(root); | |
nodeQ.push(NULL); | |
cout << root->val << " "; | |
while( !nodeQ.empty() ) | |
{ | |
TreeNode *temp = nodeQ.front(); | |
nodeQ.pop(); | |
if( temp == NULL ) | |
{ | |
if( !nodeQ.empty() ) | |
{ | |
temp = nodeQ.front(); | |
cout << temp->val << " "; | |
nodeQ.pop(); | |
nodeQ.push(NULL); | |
} | |
} | |
if( temp != NULL ) | |
{ | |
if( temp->left != NULL ) | |
nodeQ.push(temp->left); | |
if( temp->right != NULL ) | |
nodeQ.push(temp->right); | |
} | |
} | |
cout << endl; | |
} | |
void printLeftViewRecursive(TreeNode *root, int &maxLevel, int currentLevel) | |
{ | |
if( root == NULL ) | |
return; | |
if( maxLevel < currentLevel ) | |
{ | |
cout << root->val << " "; | |
maxLevel = currentLevel; | |
} | |
printLeftViewRecursive( root->left, maxLevel, currentLevel+1 ); | |
printLeftViewRecursive( root->right, maxLevel, currentLevel+1 ); | |
} | |
void leftViewRecursive(TreeNode *root) | |
{ | |
int maxLevel = -1; | |
printLeftViewRecursive(root, maxLevel, 0); | |
} | |
void Test1() | |
{ | |
TreeNode *root = NULL; | |
root = insert(root, 2); | |
root = insert(root, 1); | |
root = insert(root, 4); | |
root = insert(root, 3); | |
root = insert(root, 5); | |
printLeftView(root); | |
leftViewRecursive(root); | |
cout << endl; | |
} | |
void Test2() | |
{ | |
TreeNode *root = NULL; | |
root = insert(root, 4); | |
root = insert(root, 2); | |
root = insert(root, 5); | |
root = insert(root, 1); | |
root = insert(root, 3); | |
root = insert(root, 6); | |
printLeftView(root); | |
leftViewRecursive(root); | |
cout << endl; | |
} | |
int main() | |
{ | |
Test1(); | |
Test2(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment