剑指offer——面试题32.1:分行从上到下打印二叉树
void BFSLayer(BinaryTreeNode* pRoot) { if(pRoot==nullptr) return; queue<BinaryTreeNode*> pNode; int curLayer=1,nextLayer=0; pNode.push(pRoot); while(!pNode.empty()) { BinaryTreeNode* pFront=pNode.front(); cout<<pFront->m_Value<<' '; pNode.pop(); curLayer--; if(pFront->m_pLeft!=nullptr) { nextLayer++; pNode.push(pFront->m_pLeft); } if(pFront->m_pRight!=nullptr) { nextLayer++; pNode.push(pFront->m_pRight); } if(curLayer==0) { curLayer=nextLayer; nextLayer=0; cout<<endl; } } }
1 #include"BinaryTree.h" 2 3 // ====================测试代码==================== 4 // 8 5 // 6 10 6 // 5 7 9 11 7 void Test1() 8 { 9 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 10 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 11 BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 12 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 13 BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 14 BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 15 BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 16 17 ConnectTreeNodes(pNode8, pNode6, pNode10); 18 ConnectTreeNodes(pNode6, pNode5, pNode7); 19 ConnectTreeNodes(pNode10, pNode9, pNode11); 20 21 printf("====Test1 Begins: ====\n"); 22 printf("Expected Result is:\n"); 23 printf("8 \n"); 24 printf("6 10 \n"); 25 printf("5 7 9 11 \n\n"); 26 27 printf("Actual Result is: \n"); 28 BFSLayer(pNode8); 29 printf("\n"); 30 31 DestroyTree(pNode8); 32 } 33 34 // 5 35 // 4 36 // 3 37 // 2 38 void Test2() 39 { 40 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 41 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 42 BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 43 BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 44 45 ConnectTreeNodes(pNode5, pNode4, nullptr); 46 ConnectTreeNodes(pNode4, pNode3, nullptr); 47 ConnectTreeNodes(pNode3, pNode2, nullptr); 48 49 printf("====Test2 Begins: ====\n"); 50 printf("Expected Result is:\n"); 51 printf("5 \n"); 52 printf("4 \n"); 53 printf("3 \n"); 54 printf("2 \n\n"); 55 56 printf("Actual Result is: \n"); 57 BFSLayer(pNode5); 58 printf("\n"); 59 60 DestroyTree(pNode5); 61 } 62 63 // 5 64 // 4 65 // 3 66 // 2 67 void Test3() 68 { 69 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 70 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 71 BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); 72 BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); 73 74 ConnectTreeNodes(pNode5, nullptr, pNode4); 75 ConnectTreeNodes(pNode4, nullptr, pNode3); 76 ConnectTreeNodes(pNode3, nullptr, pNode2); 77 78 printf("====Test3 Begins: ====\n"); 79 printf("Expected Result is:\n"); 80 printf("5 \n"); 81 printf("4 \n"); 82 printf("3 \n"); 83 printf("2 \n\n"); 84 85 printf("Actual Result is: \n"); 86 BFSLayer(pNode5); 87 printf("\n"); 88 89 DestroyTree(pNode5); 90 } 91 92 void Test4() 93 { 94 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 95 96 printf("====Test4 Begins: ====\n"); 97 printf("Expected Result is:\n"); 98 printf("5 \n\n"); 99 100 printf("Actual Result is: \n"); 101 BFSLayer(pNode5); 102 printf("\n"); 103 104 DestroyTree(pNode5); 105 } 106 107 void Test5() 108 { 109 printf("====Test5 Begins: ====\n"); 110 printf("Expected Result is:\n"); 111 112 printf("Actual Result is: \n"); 113 BFSLayer(nullptr); 114 printf("\n"); 115 } 116 117 // 100 118 // / 119 // 50 120 // \ 121 // 150 122 void Test6() 123 { 124 BinaryTreeNode* pNode100 = CreateBinaryTreeNode(100); 125 BinaryTreeNode* pNode50 = CreateBinaryTreeNode(50); 126 BinaryTreeNode* pNode150 = CreateBinaryTreeNode(150); 127 128 ConnectTreeNodes(pNode100, pNode50, nullptr); 129 ConnectTreeNodes(pNode50, nullptr, pNode150); 130 131 printf("====Test6 Begins: ====\n"); 132 printf("Expected Result is:\n"); 133 printf("100 \n"); 134 printf("50 \n"); 135 printf("150 \n\n"); 136 137 printf("Actual Result is: \n"); 138 BFSLayer(pNode100); 139 printf("\n"); 140 } 141 142 int main(int argc, char* argv[]) 143 { 144 Test1(); 145 Test2(); 146 Test3(); 147 Test4(); 148 Test5(); 149 Test6(); 150 151 return 0; 152 }