IT公司100题-16-层遍历二元树

问题描述:

层遍历二叉树,同一层从左往右打印。

定义二元查找树的结点为:

typedef struct BSTreeNode {
    int data;
    BSTreeNode *left;
    BSTreeNode *right;
} Node;

例如输入二叉树:

6
 /   \
 4    12
/ \    / \
2 5 8  16

输出:6 4 12 2 5 8 16。

分析:

二叉树的广度优先遍历。

代码实现:

 1 // 16.cc
 2 #include <deque>
 3 #include <iostream>
 4 using namespace std;
 5
 6 typedef struct BSTreeNode {
 7     int data;
 8     BSTreeNode *left;
 9     BSTreeNode *right;
10 } Node;
11
12 // 创建二元查找树
13 void add_BSTree_node(Node* &p_current, int data) {
14     if (NULL == p_current) {
15         Node *node = new Node();
16         node->left = NULL;
17         node->right = NULL;
18         node->data = data;
19         p_current = node;
20     } else {
21         if (p_current->data > data)
22             add_BSTree_node(p_current->left, data);
23         else if (p_current->data < data)
24             add_BSTree_node(p_current->right, data);
25         else
26             cout << "The data has already in the Node.";
27     }
28 }
29
30 // 层遍历
31 void level_order(Node* root) {
32     deque<Node*> s;
33     s.push_back(root);
34     Node* p;
35
36     while (!s.empty()) {
37         p = s.front();
38         s.pop_front();
39         cout << p->data << " ";
40
41         if (p->left != NULL)
42             s.push_back(p->left);
43         if (p->right != NULL)
44             s.push_back(p->right);
45     }
46     cout << endl;
47 }
48
49 int main() {
50     Node *root = NULL;
51     add_BSTree_node(root, 6);
52     add_BSTree_node(root, 4);
53     add_BSTree_node(root, 12);
54     add_BSTree_node(root, 2);
55     add_BSTree_node(root, 5);
56     add_BSTree_node(root, 8);
57     add_BSTree_node(root, 16);
58
59     level_order(root);
60     return 0;
61 }

IT公司100题-16-层遍历二元树

时间: 2024-08-29 18:04:57

IT公司100题-16-层遍历二元树的相关文章

IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果

问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树的后序遍历结果: 10/     \6      14/  \    /   \4   8 12    16 因此返回true. 如果输入6, 5, 8, 5, 7 ,则返回false. 分析: 在后续遍历得到的序列中,最后一个元素为树的根结点.根节点元素将数组分为两部分,左边都小于根节点,右边都大

IT公司100题-15-求二元查找树的镜像

问题描述: 输入一颗二元查找树,将该树转换为它的镜像树,即对每一个节点,互换左右子树. 例如输入: 6/    \4     12/ \   /   \2  5 8   16 输出: 6/     \12     4/   \   / \16  8 5  2 定义二元查找树的结点为: typedef struct BSTree { int data; BSTree* left; BSTree* right; } Node; 分析: 方法1:递归交换左右子树. // 15_1.cc #includ

IT公司100题-14-排序数组中和为给定值的两个数字

问题描述: 输入一个升序排序的数组,给定一个目标值target,求数组的两个数a和b,a+b=target.如果有多个组合满足这个条件,输出任意一对即可. 例如,输入升序数组[1, 3, 4, 5, 13, 17]和目标值20.输出3和17. 分析: 最简单的办法,直接遍历,时间复杂度为O(n^2). 双下标法:low和high a[low]+a[high] < target, low++; a[low]+a[high] > target, high–; a[low]+a[high] == t

IT公司100题-13-求链表中倒数第k个结点

问题描述: 输入一个单向链表,输出该链表中倒数第k个结点.链表倒数第0个节点为NULL. struct list_node { int data; list_node* next; }; 分析: 方法1: 首先计算出链表中节点的个数n,然后倒数第k个节点,为正数n-k+1个节点. 需要遍历链表2次. 方法1代码实现: 1 // 13_1.cc 2 #include <iostream> 3 using namespace std; 4 5 struct list_node { 6 int da

IT公司100题-11-求二叉树中节点的最大距离

问题描述: 写程序,求一棵二叉树中相距最远的两个节点之间的距离. 10/     \6      14/   \   /   \4    8 12    16 分析: 二叉树中最远的两个节点,要么是根和一个叶子节点,要么是两个叶子节点. 代码实现: 1 // 11.cc 2 #include <iostream> 3 using namespace std; 4 5 typedef struct BSTreeNode { 6 int data; 7 BSTreeNode *left; 8 BS

IT公司100题-25-求字符串中的最长数字串

问题描述: 实现一个函数,求出字符串中的连续最长数字串.例如输入”12345cbf3456″,输出”12345″. 函数原型为: void conti_num_max( const char * src, char * dest); dest保存最长数字串,返回void. 分析: 遍历一遍字符串,记录起始位置和长度即可. 代码实现: 1 // 25.cc 2 #include <iostream> 3 #include <cstring> 4 using namespace std

【IT公司100题-7】-判断两个链表是否相交

问题:有一个单链表,其中可能有一个环,也就是某个节点的next指向的是链表中在它之前的节点,这样在链表的尾部形成一环.1.如何判断一个链表是不是这类链表? 问题扩展:1.如果链表可能有环呢?2.如果需要求出两个链表相交的第一个节点呢? 分析: 在无环的情况下,如果两个链表有结点相同,那么它们下一结点也相同,如此可推出尾结点也相同. 那么只要判断两链表的尾结点是否相同.(O(len1+len2)) 1 struct Node { 2 int data; 3 int Node *next; 4 };

IT公司100题-12-求1+2+…+n

问题描述: 求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 分析: 利用类的静态变量实现: new一含有n个这种类的数组,那么该类的构造函数将会被调用n次. 代码实现: 1 // 12.cc 2 #include <iostream> 3 using namespace std; 4 5 class Object { 6 public: 7 Object() { 8 ++N; 9 Sum += N; 10

IT公司100题-2

1 // 定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素. 2 // 要求函数min.push 以及pop 的时间复杂度都是O(1). 3 #include <iostream> 4 #include "../data/own/c2_list.h" 5 using namespace std; 6 7 template <typename object> 8 class miniStack{ 9 public: 10 bool isEmpty(