剑指Offer面试题:21.从上到下打印二叉树
2015-09-03 10:23
627 查看
一、题目:从上到下打印二叉树
题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印。例如输入下图中的二叉树,则依次打印出8、6、10、5、7、9、11。static void TestPortal(string testName, BinaryTreeNode root) { if (!string.IsNullOrEmpty(testName)) { Console.WriteLine("{0} begins:", testName); } Console.WriteLine("The nodes from top to bottom, from left to right are:"); PrintFromTopToBottom(root); Console.WriteLine("\n"); } static void SetSubTreeNode(BinaryTreeNode root, BinaryTreeNode lChild, BinaryTreeNode rChild) { if (root == null) { return; } root.leftChild = lChild; root.rightChild = rChild; } static void ClearUpTreeNode(BinaryTreeNode root) { if(root != null) { BinaryTreeNode left = root.leftChild; BinaryTreeNode right = root.rightChild; root = null; ClearUpTreeNode(left); ClearUpTreeNode(right); } }
View Code
3.1 功能测试
// 10 // / \ // 6 14 // /\ /\ // 4 8 12 16 static void Test1() { BinaryTreeNode node10 = new BinaryTreeNode(10); BinaryTreeNode node6 = new BinaryTreeNode(6); BinaryTreeNode node14 = new BinaryTreeNode(14); BinaryTreeNode node4 = new BinaryTreeNode(4); BinaryTreeNode node8 = new BinaryTreeNode(8); BinaryTreeNode node12 = new BinaryTreeNode(12); BinaryTreeNode node16 = new BinaryTreeNode(16); SetSubTreeNode(node10, node6, node14); SetSubTreeNode(node6, node4, node8); SetSubTreeNode(node14, node12, node16); TestPortal("Test1", node10); ClearUpTreeNode(node10); } // 5 // / // 4 // / // 3 // / // 2 // / // 1 static void Test2() { BinaryTreeNode node5 = new BinaryTreeNode(5); BinaryTreeNode node4 = new BinaryTreeNode(4); BinaryTreeNode node3 = new BinaryTreeNode(3); BinaryTreeNode node2 = new BinaryTreeNode(2); BinaryTreeNode node1 = new BinaryTreeNode(1); node5.leftChild = node4; node4.leftChild = node3; node3.leftChild = node2; node2.leftChild = node1; TestPortal("Test2", node5); ClearUpTreeNode(node5); } // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 static void Test3() { BinaryTreeNode node5 = new BinaryTreeNode(5); BinaryTreeNode node4 = new BinaryTreeNode(4); BinaryTreeNode node3 = new BinaryTreeNode(3); BinaryTreeNode node2 = new BinaryTreeNode(2); BinaryTreeNode node1 = new BinaryTreeNode(1); node1.rightChild = node2; node2.rightChild = node3; node3.rightChild = node4; node4.rightChild = node5; TestPortal("Test3", node1); ClearUpTreeNode(node5); } // 树中只有1个结点 static void Test4() { BinaryTreeNode node1 = new BinaryTreeNode(1); TestPortal("Test4", node1); ClearUpTreeNode(node1); } // 树中木有结点 static void Test5() { TestPortal("Test5", null); }
3.2 测试结果
作者:周旭龙
出处:http://edisonchou.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
相关文章推荐
- 面试总结
- 14条职场生涯建议
- 剑指offer——面试题38:数字在排序数组中出现的次数(利用二分查找来找第一次和最后一次的位置)
- 初学者应该解决的设计模式面试问题
- 面试之线程篇
- 黑马程序员——Html
- 程序员找工作需要看的书
- java基础面试题(四)
- Java研发求职指南
- 剑指Offer面试题:20.栈的压入、弹出序列
- 程序员噩梦 13 种,你遇到过哪些?
- 黑马程序员——多线程10:多线程相关练习
- 《剑指Offer》面试题6 重建二叉树——勘误
- 面试常考之二叉树(转载)
- 操作系统之面试常考(后期补充)
- 黑马程序员——多线程9:其他线程技术-下
- 黑马程序员——43,String,StringBuffer ,基本数据类型包装对象
- 嵌入式程序员应知道的0x10个基本问题
- 一个google面试的准备清单
- 黑马程序员——多线程8:其他线程工具—上