微软百题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 ); } } }