这道题目一共两问,其一如何判断一颗二叉树是否是平衡二叉树?平衡二叉树指任意一父节点左右字数高度差不大于1,就是说<=1即可。高度,自然有一个求属深度的方法,所以有了如下内容:
bool IsBalance()
{
return _IsBalance(_root);
}
int depth()
{
return _depth(_root);
}
int _depth(Node* root)
{
if (root == NULL)
return 0;
int leftheight = _depth(root->_left) + 1;
int rightheight = _depth(root->_right) + 1;
return leftheight > rightheight ? leftheight : rightheight;
}
bool _IsBalance(Node* root)
{
if (root == NULL)
return true;
int leftheight = _depth(root->_left);
int rightheight = _depth(root->_right);
int is_balance = leftheight - rightheight;
if (is_balance <= 1 && is_balance >= -1)
return _IsBalance(root->_left) && _IsBalance(root->_right);
else
return false;
}
第二问,求一棵树的镜像?也就是说每一个节点映射到对应的相反的位置。
void BinaryMirror1()
{
_BinaryMirror(_root);
}
void BinaryMirror2()
{
if (_root == NULL)
return;
queue<Node*> q;
q.push(_root);
while (!q.empty())
{
Node* cur = q.front();
swap(cur->_left, cur->_right);
q.pop();
if (cur->_left)
q.push(cur->_left);
if (cur->_right)
q.push(cur->_right);
}
}
void _BinaryMirror(Node* root)
{
if (root != NULL)
{
swap(root->_left, root->_right);
_BinaryMirror(root->_left);
_BinaryMirror(root->_right);
}
}
两个问题的完整代码
#define _CRT_SECURE_NO_WARNINGS 10
#include<iostream>
#include<stdlib.h>
#include<queue>
using namespace std;
template<class T>
struct TreeNode{
T _data;
TreeNode<T>* _left;
TreeNode<T>* _right;
TreeNode(T x = T())
:_data(x)
, _left(NULL)
, _right(NULL)
{}
};
template<class T>
class Tree{
typedef TreeNode<T> Node;
public:
Tree()
:_root(NULL)
{}
Tree(const T* arr, size_t size, const size_t invalued)
{
size_t index = 0;
_CreateTree(_root,arr, size, index, invalued);
}
void PrevOrder()
{
cout << "InOrder:";
_PrevOrder(_root);
cout << endl;
}
bool IsBalance()
{
return _IsBalance(_root);
}
int depth()
{
return _depth(_root);
}
void BinaryMirror1()
{
_BinaryMirror(_root);
}
void BinaryMirror2()
{
if (_root == NULL)
return;
queue<Node*> q;
q.push(_root);
while (!q.empty())
{
Node* cur = q.front();
swap(cur->_left, cur->_right);
q.pop();
if (cur->_left)
q.push(cur->_left);
if (cur->_right)
q.push(cur->_right);
}
}
protected:
void _CreateTree(Node*& root,const T arr[], size_t size, size_t& index, T invalued)
{
if (index < size&&arr[index] != invalued)
{
root = new Node(arr[index]);
_CreateTree(root->_left, arr, size, ++index, invalued);
_CreateTree(root->_right, arr, size, ++index, invalued);
}
}
void _PrevOrder(Node* root)
{
if (root == NULL)
return;
cout << root->_data << " ";
_PrevOrder(root->_left);
_PrevOrder(root->_right);
}
int _depth(Node* root)
{
if (root == NULL)
return 0;
int leftheight = _depth(root->_left) + 1;
int rightheight = _depth(root->_right) + 1;
return leftheight > rightheight ? leftheight : rightheight;
}
bool _IsBalance(Node* root)
{
if (root == NULL)
return true;
int leftheight = _depth(root->_left);
int rightheight = _depth(root->_right);
int is_balance = leftheight - rightheight;
if (is_balance <= 1 && is_balance >= -1)
return _IsBalance(root->_left) && _IsBalance(root->_right);
else
return false;
}
void _BinaryMirror(Node* root)
{
if (root != NULL)
{
swap(root->_left, root->_right);
_BinaryMirror(root->_left);
_BinaryMirror(root->_right);
}
}
private:
Node* _root;
};
int main()
{
int arr[20] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
int size = sizeof(arr) / sizeof(arr[10]);
Tree<int> tree(arr,10,'#');
tree.PrevOrder();
if (int ret = tree.IsBalance())
cout << "ture" << endl;
else
cout << "false" << endl;
tree.BinaryMirror2();
tree.PrevOrder();
system("pause");
return 0;
}