Please post your answer in comments if you have a better solution.
clear(struct node* pNode){ if (pNode != NULL) { clear(pNode->left); clear(pNode->right); delete pNode; }}
Problems and solutions are from:http://cslibrary.stanford.edu/110/BinaryTrees.html
here c interview questions are useful for students...nice article..thanks for the author...can you send me more c puzzles..
Thanks for the info. It is very helpful.RegardsEducational siteGet jobs info at Educational site
For more challenge, do it iteratively :)void DeleteTreeIter( TreeNode* root ){ if( !root ) { return; } TreeNode* curr = root; Stack s; while( !s.IsEmpty() || curr ) { if( curr ) { if( curr->Right ) { s.Push( curr->Right ); } s.Push( curr ); curr = curr->Left; } else { curr = s.Pop(); if( curr->Right == s.Peek() ) { TreeNode* temp = s.pop(); s.Push( curr ); curr = temp; } else { delete curr; curr = NULL; } } }}
Post a Comment
5 comments:
clear(struct node* pNode)
{
if (pNode != NULL)
{
clear(pNode->left);
clear(pNode->right);
delete pNode;
}
}
Problems and solutions are from:
http://cslibrary.stanford.edu/110/BinaryTrees.html
here c interview questions are useful for students...
nice article..thanks for the author...
can you send me more c puzzles..
Thanks for the info. It is very helpful.
Regards
Educational site
Get jobs info at Educational site
For more challenge, do it iteratively :)
void DeleteTreeIter( TreeNode* root )
{
if( !root ) { return; }
TreeNode* curr = root;
Stack s;
while( !s.IsEmpty() || curr )
{
if( curr )
{
if( curr->Right )
{
s.Push( curr->Right );
}
s.Push( curr );
curr = curr->Left;
}
else
{
curr = s.Pop();
if( curr->Right == s.Peek() )
{
TreeNode* temp = s.pop();
s.Push( curr );
curr = temp;
}
else
{
delete curr;
curr = NULL;
}
}
}
}
Post a Comment