二叉树层序遍历
二叉树:
层序遍历图:A->B->C->D->E
层序遍历思想:
建立一个队列,把树根入队列,然后把队头元素打印并删掉,然后把它的左右结点依次入队,如果结点为空的话就不入队,依次这样操作,当队列为空时,就完成层序遍历了。
比如上图的层序遍历
1.把树根A入队
然后把队头A删掉,把队头的左右BC结点依次入队
继续重复上面动作,即把队头B删掉,把它的指向左右结点DE依次入队
然后删掉队头C,但它的左右结点都是空,所以不用入队
然后把队头D删掉,它的左右结点为空,不用入队
然后把队头E删掉,它的左右结点为空,不用入队
此时队列为空了,就表示层序遍历完成了
代码实现
#include <iostream>
#include <assert.h>
using namespace std;
struct TreeNode
{
int _val;
TreeNode* _left;
TreeNode* _right;
TreeNode(int val = 0)
:_val(val)
,_left(nullptr)
,_right(nullptr)
{}
};
struct QueueNode
{
TreeNode* _val;
QueueNode* _next;
QueueNode(TreeNode* val = nullptr)
:_val(val)
,_next(nullptr)
{}
};
class Queue
{
public:
Queue()
:_head(nullptr)
,_tail(nullptr)
{}
~Queue()
{
QueueNode* cur = _head;
while (cur)
{
QueueNode* next = cur->_next;
delete cur;
cur = next;
}
_head = _tail = nullptr;
}
bool empty()
{
return _head == nullptr;
}
size_t size()
{
size_t size = 0;
QueueNode* cur = _head;
while (cur)
{
size++;
cur = cur->_next;
}
return size;
}
TreeNode* front()
{
assert(_head);
return _head->_val;
}
TreeNode* back()
{
assert(_head);
return _tail->_val;
}
void push_back(TreeNode* val)
{
QueueNode* newNode = new QueueNode(val);
if (_head == nullptr)
{
_head = _tail = newNode;
}
else
{
_tail->_next = newNode;
_tail = newNode;
}
}
void pop_front()
{
assert(_head);
if (_head->_next == nullptr)
{
delete _head;
_head = _tail = nullptr;
}
else
{
QueueNode* newHead = _head->_next;
delete _head;
_head = newHead;
}
}
private:
QueueNode* _head;
QueueNode* _tail;
};
//层序遍历
void level_order(TreeNode* root)
{
if (root == nullptr)
return;
Queue q;
//把根结点入队
q.push_back(root);
while (!q.empty())
{
//打印队头结点
TreeNode* front = q.front();
cout << front->_val << "->";
//删除队头结点
q.pop_front();
//如果队头的左右结点不为空,则把它们依次入队
if(front->_left)
q.push_back(front->_left);
if(front->_right)
q.push_back(front->_right);
}
}
int main()
{
TreeNode* a = new TreeNode(1);
TreeNode* b = new TreeNode(2);
TreeNode* c = new TreeNode(3);
TreeNode* d = new TreeNode(4);
TreeNode* e = new TreeNode(5);
TreeNode* f = new TreeNode(6);
a->_left = b;
a->_right = c;
b->_left = d;
b->_right = e;
c->_right = f;
level_order(a);
return 0;
}
判断二叉树是否是完全二叉树
思路:和遍历二叉树相似,也是用队列解决,先把树根入队,然后标记队头并把队头删除,把队头的左右结点依次入队列,但是当队列为空的时候,停止操作,检查队列剩余结点,如果结点都是空是则是完全二叉树,否则不是完全二叉树
代码实现:
#include <iostream>
#include <assert.h>
using namespace std;
struct TreeNode
{
int _val;
TreeNode* _left;
TreeNode* _right;
TreeNode(int val = 0)
:_val(val)
,_left(nullptr)
,_right(nullptr)
{}
};
struct QueueNode
{
TreeNode* _val;
QueueNode* _next;
QueueNode(TreeNode* val = nullptr)
:_val(val)
,_next(nullptr)
{}
};
class Queue
{
public:
Queue()
:_head(nullptr)
,_tail(nullptr)
{}
~Queue()
{
QueueNode* cur = _head;
while (cur)
{
QueueNode* next = cur->_next;
delete cur;
cur = next;
}
_head = _tail = nullptr;
}
bool empty()
{
return _head == nullptr;
}
size_t size()
{
size_t size = 0;
QueueNode* cur = _head;
while (cur)
{
size++;
cur = cur->_next;
}
return size;
}
TreeNode* front()
{
assert(_head);
return _head->_val;
}
TreeNode* back()
{
assert(_head);
return _tail->_val;
}
void push_back(TreeNode* val)
{
QueueNode* newNode = new QueueNode(val);
if (_head == nullptr)
{
_head = _tail = newNode;
}
else
{
_tail->_next = newNode;
_tail = newNode;
}
}
void pop_front()
{
assert(_head);
if (_head->_next == nullptr)
{
delete _head;
_head = _tail = nullptr;
}
else
{
QueueNode* newHead = _head->_next;
delete _head;
_head = newHead;
}
}
private:
QueueNode* _head;
QueueNode* _tail;
};
//判断是否是完全二叉树
bool is_complete_tree(TreeNode* root)
{
//空树是完全二叉树
if (root == nullptr)
return true;
Queue q;
q.push_back(root);
while (!q.empty())
{
TreeNode* front = q.front();
q.pop_front();
if (front == nullptr)
break;
q.push_back(front->_left);
q.push_back(front->_right);
}
//遍历队列剩余结点
while (!q.empty())
{
//如果出现非空结点,则不是完全二叉树
if (q.front() != nullptr)
return false;
q.pop_front();
}
//遍历完没有返回false,表示队列都是空,则是完全二叉树
return true;
}
int main()
{
TreeNode* a = new TreeNode(1);
TreeNode* b = new TreeNode(2);
TreeNode* c = new TreeNode(3);
TreeNode* d = new TreeNode(4);
TreeNode* e = new TreeNode(5);
TreeNode* f = new TreeNode(6);
a->_left = b;
a->_right = c;
b->_left = d;
b->_right = e;
c->_right = f;
cout << "是否是完全二叉树:" << is_complete_tree(a) << endl;
return 0;
}