微软百题16:按树的层次打印节点

typedef struct TreeNode
{
int value;
TreeNode * left;
TreeNode * right;
}TreeNode;


void CreateTree( TreeNode * & pTreeNode, int value)
{
if( pTreeNode == NULL)
{
pTreeNode = (TreeNode *) new TreeNode;
pTreeNode ->left = NULL;
pTreeNode ->right = NULL;
pTreeNode ->value = value;
}
else 
if( pTreeNode ->value > value)
{
CreateTree( pTreeNode ->left ,value);
}
if(pTreeNode ->value < value)
{
CreateTree(pTreeNode ->right, value);
}

return ;
}
void PrintNode(TreeNode * pTreeNode)
{
if (pTreeNode == NULL)
return ;
deque<TreeNode * > treeDeque;
treeDeque.push_back(pTreeNode);

while(treeDeque.size())
{
TreeNode * pTemp = treeDeque.front();
printf("%d\t", treeDeque.front()->value);
treeDeque.pop_front();

if(pTemp->left)
{
treeDeque.push_back(pTemp->left);
}
if( pTemp->right )
{
treeDeque.push_back(pTemp->right );
}

}

}

 

posted @ 2013-08-11 16:24  sorgin  阅读(153)  评论(0编辑  收藏  举报