当前位置: 首页 > news >正文

分行从上到下打印二叉树-C++

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程https://www.captainai.net/troubleshooter

// 面试题32(二):分行从上到下打印二叉树 // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层 // 打印到一行。 #include <cstdio> #include <queue> struct BinaryTreeNode { int m_nValue; BinaryTreeNode *m_pLeft; BinaryTreeNode *m_pRight; }; BinaryTreeNode *CreateBinaryTreeNode(int value) { BinaryTreeNode *pNode = new BinaryTreeNode(); pNode->m_nValue = value; pNode->m_pLeft = nullptr; pNode->m_pRight = nullptr; return pNode; } void ConnectTreeNodes(BinaryTreeNode *pParent, BinaryTreeNode *pLeft, BinaryTreeNode *pRight) { if (pParent != nullptr) { pParent->m_pLeft = pLeft; pParent->m_pRight = pRight; } } void PrintTreeNode(const BinaryTreeNode *pNode) { if (pNode != nullptr) { printf("value of this node is: %d\n", pNode->m_nValue); if (pNode->m_pLeft != nullptr) printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue); else printf("left child is nullptr.\n"); if (pNode->m_pRight != nullptr) printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue); else printf("right child is nullptr.\n"); } else { printf("this node is nullptr.\n"); } printf("\n"); } void PrintTree(const BinaryTreeNode *pRoot) { PrintTreeNode(pRoot); if (pRoot != nullptr) { if (pRoot->m_pLeft != nullptr) PrintTree(pRoot->m_pLeft); if (pRoot->m_pRight != nullptr) PrintTree(pRoot->m_pRight); } } void DestroyTree(BinaryTreeNode *pRoot) { if (pRoot != nullptr) { BinaryTreeNode *pLeft = pRoot->m_pLeft; BinaryTreeNode *pRight = pRoot->m_pRight; delete pRoot; pRoot = nullptr; DestroyTree(pLeft); DestroyTree(pRight); } } void Print(BinaryTreeNode *pRoot) { if (pRoot == nullptr) return; std::queue<BinaryTreeNode *> nodes; nodes.push(pRoot); int nextLevel = 0; int toBePrinted = 1; while (!nodes.empty()) { BinaryTreeNode *pNode = nodes.front(); printf("%d ", pNode->m_nValue); if (pNode->m_pLeft != nullptr) { nodes.push(pNode->m_pLeft); ++nextLevel; } if (pNode->m_pRight != nullptr) { nodes.push(pNode->m_pRight); ++nextLevel; } nodes.pop(); --toBePrinted; if (toBePrinted == 0) { printf("\n"); toBePrinted = nextLevel; nextLevel = 0; } } } // ====================测试代码==================== // 8 // 6 10 // 5 7 9 11 void Test1() { BinaryTreeNode *pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode *pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode *pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode *pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode *pNode9 = CreateBinaryTreeNode(9); BinaryTreeNode *pNode11 = CreateBinaryTreeNode(11); ConnectTreeNodes(pNode8, pNode6, pNode10); ConnectTreeNodes(pNode6, pNode5, pNode7); ConnectTreeNodes(pNode10, pNode9, pNode11); printf("====Test1 Begins: ====\n"); printf("Expected Result is:\n"); printf("8 \n"); printf("6 10 \n"); printf("5 7 9 11 \n\n"); printf("Actual Result is: \n"); Print(pNode8); printf("\n"); DestroyTree(pNode8); } // 5 // 4 // 3 // 2 void Test2() { BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2); ConnectTreeNodes(pNode5, pNode4, nullptr); ConnectTreeNodes(pNode4, pNode3, nullptr); ConnectTreeNodes(pNode3, pNode2, nullptr); printf("====Test2 Begins: ====\n"); printf("Expected Result is:\n"); printf("5 \n"); printf("4 \n"); printf("3 \n"); printf("2 \n\n"); printf("Actual Result is: \n"); Print(pNode5); printf("\n"); DestroyTree(pNode5); } // 5 // 4 // 3 // 2 void Test3() { BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode *pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode *pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode *pNode2 = CreateBinaryTreeNode(2); ConnectTreeNodes(pNode5, nullptr, pNode4); ConnectTreeNodes(pNode4, nullptr, pNode3); ConnectTreeNodes(pNode3, nullptr, pNode2); printf("====Test3 Begins: ====\n"); printf("Expected Result is:\n"); printf("5 \n"); printf("4 \n"); printf("3 \n"); printf("2 \n\n"); printf("Actual Result is: \n"); Print(pNode5); printf("\n"); DestroyTree(pNode5); } void Test4() { BinaryTreeNode *pNode5 = CreateBinaryTreeNode(5); printf("====Test4 Begins: ====\n"); printf("Expected Result is:\n"); printf("5 \n\n"); printf("Actual Result is: \n"); Print(pNode5); printf("\n"); DestroyTree(pNode5); } void Test5() { printf("====Test5 Begins: ====\n"); printf("Expected Result is:\n"); printf("Actual Result is: \n"); Print(nullptr); printf("\n"); } // 100 // / // 50 // \ // 150 void Test6() { BinaryTreeNode *pNode100 = CreateBinaryTreeNode(100); BinaryTreeNode *pNode50 = CreateBinaryTreeNode(50); BinaryTreeNode *pNode150 = CreateBinaryTreeNode(150); ConnectTreeNodes(pNode100, pNode50, nullptr); ConnectTreeNodes(pNode50, nullptr, pNode150); printf("====Test6 Begins: ====\n"); printf("Expected Result is:\n"); printf("100 \n"); printf("50 \n"); printf("150 \n\n"); printf("Actual Result is: \n"); Print(pNode100); printf("\n"); } int main(int argc, char *argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); return 0; } // ------ Output ------ /* ====Test1 Begins: ==== Expected Result is: 8 6 10 5 7 9 11 Actual Result is: 8 6 10 5 7 9 11 ====Test2 Begins: ==== Expected Result is: 5 4 3 2 Actual Result is: 5 4 3 2 ====Test3 Begins: ==== Expected Result is: 5 4 3 2 Actual Result is: 5 4 3 2 ====Test4 Begins: ==== Expected Result is: 5 Actual Result is: 5 ====Test5 Begins: ==== Expected Result is: Actual Result is: ====Test6 Begins: ==== Expected Result is: 100 50 150 Actual Result is: 100 50 150 */
http://www.jsqmd.com/news/713371/

相关文章:

  • 【IEEE出版,西安欧亚学院主办】第六届计算机技术与信息科学国际研讨会(ISCTIS 2026)
  • 2026贵阳南明区炭火烤肉与烤鱼品质对标:正宗铁签烤肉vs竹签烤肉全方位对比指南 - 年度推荐企业名录
  • 2026年江苏阻燃面料品牌深度观察:芳纶与碳纶不燃面料厂家竞争力对比 - 速递信息
  • 2026年2月 | 企业人才培养咨询TOP8推荐 - 资讯焦点
  • 2026年新疆家具代加工与本地定制衣柜橱柜完全避坑手册 - 精选优质企业推荐官
  • Creating a Documents Window - Part 1-使文档能够在 V5 应用程序框架中显示
  • 视频内容智能提炼:让每一帧知识都触手可及
  • 2026年江苏口碑好的浇筑母线制造厂推荐,专业厂商全解析哪家好 - 工业品牌热点
  • DuckDB向量搜索:如何实现高效的相似性查询
  • 2026美白祛斑精华实测|常天然精华多肤质适配,淡斑祛黄+强韧修护,平价好用不踩雷 - 资讯焦点
  • PyFlux时间序列预测实战:金融、经济、气象数据案例分析
  • 2026年丙级防火门供应商推荐,这些厂家服务专业可放心选 - 工业品牌热点
  • SHAP值计算太慢?深入源码为你剖析性能瓶颈与优化技巧
  • 之字形打印二叉树-C++
  • 2026年贵阳南明区正宗铁签炭火烤肉与烤鱼品鉴指南 - 年度推荐企业名录
  • 2026贵阳南明区炭火烤肉、烤鱼夜宵正宗老味道品鉴(含官方联系方式) - 年度推荐企业名录
  • 2026年3月花灯产品推荐,互动花灯/夜景布置灯/景区灯会/宫灯/氛围装饰灯/水上花灯/大型户外花灯,花灯企业哪家好 - 品牌推荐师
  • 别再只会Next了!Git 2.39.2 Windows安装时这7个选项到底怎么选?保姆级解读
  • 2026年贵阳炭火烤肉与竹签烤肉选购指南:5大品牌深度横评 - 年度推荐企业名录
  • Rustonomicon 实战:如何编写零成本抽象的高性能代码
  • 3步搞定OBS RTSP直播:obs-rtspserver插件完全指南
  • 解读2026年洛阳好用的物业公司,商场与医院物业怎么选 - 工业品牌热点
  • 如何将PDF转长图?免费导出无水印格式
  • 深入GitX历史浏览功能:完整代码提交可视化解决方案
  • 2026贵阳花果园正宗铁签炭火烤肉与竹签烤鱼夜宵选择指南 - 年度推荐企业名录
  • QtC++使用QRunnable+QThreadPool管理多线程
  • 2026最新面霜/面膜公司/供应商/厂家推荐!国内优质榜单发布,广东广州等地实力厂商精选 - 十大品牌榜
  • 湖北音响改装哪家强?2026年04月精选门店推荐,宝马原厂音响升级/宝马音响改装,音响改装旗舰店哪家可靠 - 品牌推荐师
  • 2026 综合型私域电商平台排名|五大优质平台深度推荐 - 速递信息
  • 避开这3个坑,你的PSIM Boost电路仿真结果才准(以12V转36V为例)