从上往下打印二叉树
Posted 三颗心
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从上往下打印二叉树相关的知识,希望对你有一定的参考价值。
题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印。
思路:每一次打印一个结点的时候,如果该结点有子结点,则把该结点的子结点放到一个队列的末尾。接下来到队列的头部取出最早进入队列的结点,重复前面的打印操作,直至队列中所有的结点都被打印出来为止。
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 // 10 109 // / \\ 110 // 6 14 111 // /\\ /\\ 112 // 4 8 12 16 113 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 }
以上是关于从上往下打印二叉树的主要内容,如果未能解决你的问题,请参考以下文章