Left Fortune law topics 6 basis class4- determine whether a binary tree is a balanced binary tree

1. Title: determining whether a binary tree is a balanced binary tree

2. Analysis

Concept (1) balanced binary tree

Refers to any balanced binary tree node for which the height difference of about not more than 1 node. Full binary tree must be balanced binary tree, balanced binary tree is not necessarily a full binary tree.
Here Insert Picture Description
For about 4,5 node are empty, the height is 0, the difference is in line with 0;
for node 2, the left height is 0, 1 the right height, a difference is satisfied;
for node 3, a height of the left and right height 0, a difference is satisfied;
for node 1, a height of 2 left and right height 2, the difference becomes satisfied;
therefore the above-described balanced binary tree.
Here Insert Picture Description
After increasing node 6, to node 3, a height of 2 left and right height is 0, do not match.

(2) binary tree problem loosened Road

(Dp problem tree can be read)
- the use of binary tree recursive function to solve certain problems, for this question, all nodes are balanced, then the whole tree pieces balance.

  • 1
    For the information of the current node x is the following (listed possibilities):
    ① left balance No: left unbalance the whole pieces unbalanced tree
    ② Right balance NO: Right unbalance unbalance the whole pieces of trees
    in equilibrium are then premise Analyzing the difference in height, the height of the required information about sub-trees:
    ③ left high
    ④ right high
  • 2
    design Back recursive structure: the need to balance the recursion information and height information
  • 3
    write code

3. core code

Establish (1) of the tree

//树结构
class Tree
{
public:
	int val;
	Tree* left;
	Tree* right;
	Tree(int num)
	{
		this->val = num;
		right = NULL;
		left = NULL;
	}
};
//main函数中
	Tree *head = new Tree(1);
	head->left = new Tree(2);
	head->left->right = new Tree(4);
	head->right = new Tree(3);
	head->right->left = new Tree(5);
	head->right->left->right= new Tree(6);

(2) Design of recursive return structure

Information needs and whether the balance of height information (true, false)

//返回信息类
class returnData
{
public:
	int h;		//高度
	bool isB;	//平衡否
	returnData(bool isB,int h)//构造函数
	{
		this->h = h;
		this->isB = isB;
	}
};

(3) the recursion

Ideas: the whole process as a black box, assuming the left tree information can be obtained, then get the right tree information, seeking for two subtrees and determines the difference in height, the last back up.
Add some details of the entire process, beginning when ① If the current node is empty, return balance true, height 0; ② get left after sub-tree information if the information is false balance tree directly returns false, the right tree is the same reason; ③ Finally, if the height difference is not greater than 1, indicating that the current node is balanced, the information returns to the parent node is an equilibrium true, the height of the maximum height of the current node plus the current node (+1)

returnData* process(Tree* head)
{
	if(head == NULL)//空树平衡,高度为0
		return new returnData(true,0);
	
	returnData* leftData = process(head->left);//假设可以在左树收到信息	
	if(!leftData->isB)//已经不平衡直接返回
		return new returnData(false,0);
	
	returnData* rightData = process(head->right);//假设可以在右树收到信息
	if(!rightData->isB)//已经不平衡直接返回
		return new returnData(false,0);

	//在都平衡时求高度差
	if(abs(leftData->h - rightData->h) > 1)
		return new returnData(false,0);
	
	//返回左右树最高高度加1
	return new returnData(true,max(leftData->h , rightData->h) + 1);
}

(4) judging function

Information we only need a recursive process of balancing

bool isBalance(Tree* head)
{
	return process(head)->isB;
}

4. The complete code

#include<iostream>
using namespace std;

//树结构
class Tree
{
public:
	int val;
	Tree* left;
	Tree* right;
	Tree(int num)
	{
		this->val = num;
		right = NULL;
		left = NULL;
	}
};

//需要返回的信息
class returnData
{
public:
	int h;		//高度
	bool isB;	//平衡否
	returnData(bool isB,int h)//构造函数
	{
		this->h = h;
		this->isB = isB;
	}
};

//递归
returnData* process(Tree* head)
{
	if(head == NULL)//空树平衡,高度为0
		return new returnData(true,0);
	
	returnData* leftData = process(head->left);//假设可以在左树收到信息	
	if(!leftData->isB)//已经不平衡直接返回
		return new returnData(false,0);
	
	returnData* rightData = process(head->right);//假设可以在右树收到信息
	if(!rightData->isB)//已经不平衡直接返回
		return new returnData(false,0);

	//在都平衡时求高度差
	if(abs(leftData->h - rightData->h) > 1)
		return new returnData(false,0);
	
	//返回左右树最高高度加1
	return new returnData(true,max(leftData->h , rightData->h) + 1);
}

//判断函数
bool isBalance(Tree* head)
{
	return process(head)->isB;
}

int main()
{
	Tree *head = new Tree(1);
	head->left = new Tree(2);
	head->left->right = new Tree(4);
	head->right = new Tree(3);
	head->right->left = new Tree(5);
	head->right->left->right= new Tree(6);

	cout<<isBalance(head);
	
	system("pause");
	return 0;
}
Published 51 original articles · won praise 1 · views 1373

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/104000778