Print the binary tree from top to bottom and print the binary tree from bottom to bottom.
Question: each node of the binary tree is printed from top to bottom, and the nodes on the same layer are printed from left to right.
Idea: If a node has a subnode every time a node is printed, the subnode of the node is placed at the end of a queue. Next, retrieve the first node in the queue from the queue header and repeat the previous print operation until all nodes in the queue are printed.
1 #include "stdafx.h" 2 #include<stdio.h> 3 #include<deque> 4 #include<tchar.h> 5 6 struct BinaryTreeNode 7 { 8 int m_nValue; 9 BinaryTreeNode* m_pLeft; 10 BinaryTreeNode* m_pRight; 11 }; 12 13 BinaryTreeNode* CreateBinaryTreeNode(int value) 14 { 15 BinaryTreeNode* pNode = new BinaryTreeNode(); 16 pNode->m_nValue = value; 17 pNode->m_pLeft = NULL; 18 pNode->m_pRight = NULL; 19 } 20 21 void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, 22 BinaryTreeNode* pRight) 23 { 24 if(pParent != NULL) 25 { 26 pParent->m_pLeft = pLeft; 27 pParent->m_pRight = pRight; 28 } 29 } 30 31 void PrintTreeNode(BinaryTreeNode* pNode) 32 { 33 if(pNode != NULL) 34 { 35 printf("value of this node is: %d\n", pNode->m_nValue); 36 37 if(pNode->m_pLeft != NULL) 38 printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue); 39 else 40 printf("left child is null.\n"); 41 42 if(pNode->m_pRight != NULL) 43 printf("value of its right child is: %d.\n",pNode->m_pRight->m_nValue); 44 else 45 printf("right child is null.\n"); 46 } 47 else 48 { 49 printf("this node is null.\n"); 50 } 51 printf("\n"); 52 } 53 54 void PrintTree(BinaryTreeNode* pRoot) 55 { 56 PrintTreeNode(pRoot); 57 58 if(pRoot != NULL) 59 { 60 if(pRoot->m_pLeft != NULL) 61 PrintTree(pRoot->m_pLeft); 62 63 if(pRoot->m_pRight != NULL) 64 PrintTree(pRoot->m_pRight); 65 } 66 } 67 68 void DestroyTree(BinaryTreeNode* pRoot) 69 { 70 if(pRoot != NULL) 71 { 72 BinaryTreeNode* pLeft = pRoot->m_pLeft; 73 BinaryTreeNode* pRight = pRoot->m_pRight; 74 75 delete pRoot; 76 pRoot = NULL; 77 78 DestroyTree(pLeft); 79 DestroyTree(pRight); 80 } 81 } 82 83 84 void PrintFromTopToBottom(BinaryTreeNode* pRoot) 85 { 86 if(pRoot == NULL) 87 return; 88 89 std::deque<BinaryTreeNode *> dequeTreeNode; 90 91 dequeTreeNode.push_back(pRoot); 92 93 while(dequeTreeNode.size()) 94 { 95 BinaryTreeNode *pNode = dequeTreeNode.front(); 96 dequeTreeNode.pop_front(); 97 98 printf("%d ", pNode->m_nValue); 99 100 if(pNode->m_pLeft)101 dequeTreeNode.push_back(pNode->m_pLeft);102 103 if(pNode->m_pRight)104 dequeTreeNode.push_back(pNode->m_pRight);105 }106 }107 108 // 10109 // / \110 // 6 14111 // /\ /\112 // 4 8 12 16113 114 int main()115 {116 BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);117 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);118 BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);119 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);120 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);121 BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);122 BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16);123 124 ConnectTreeNodes(pNode10, pNode6, pNode14);125 ConnectTreeNodes(pNode6, pNode4, pNode8);126 ConnectTreeNodes(pNode14, pNode12, pNode16);127 128 PrintTree(pNode10);129 130 printf("The nodes from top to bottom, from left to right are: \n");131 PrintFromTopToBottom(pNode10);132 133 printf("\n\n");134 }