1.二叉树结点的数据结构
struct BinaryTreeNode {
int m_nValue;
BinaryTreeNode* m_pLeftChild;
BinaryTreeNode* m_pRightChild;
};
2.二叉树的深度(递归式)
int binaryTreeDepth(BinaryTreeNode* rootNode) {
if (NULL == rootNode) {
return 0;
}
int leftChildTreeDepth = binaryTreeDepth(rootNode->m_pLeftChild);
int rightChildTreeDepth = binaryTreeDepth(rootNode->m_pRightChild);
return (leftChildTreeDepth > rightChildTreeDepth) ?
(leftChildTreeDepth + 1) : (rightChildTreeDepth + 1);
}
3.判断一个二叉树是否是平衡二叉树
bool isAVLTree(BinaryTreeNode* rootNode) {
if (NULL == rootNode) {
return true;
}
int leftChildTreeDepth = binaryTreeDepth(rootNode->m_pLeftChild);
int rightChildeTreeDepth = binaryTreeDepth(rootNode->m_pRightChild);
if (abs(leftChildTreeDepth - rightChildeTreeDepth) > 1) {
return false;
}
return isAVLTree(rootNode->m_pLeftChild)
&& isAVLTree(rootNode->m_pRightChild);
}