题目:输入一棵二叉树的根结点,判断该树是不是平衡二叉树

来源:互联网 发布:烟台华商网络怎么样 编辑:程序博客网 时间:2024/04/30 07:38

题目:输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。例如,下图中的二叉树就是一棵平衡二叉树。


 


解题思路:

1)需要重复遍历节点多次的解法

 

      在上一篇博客中(http://blog.csdn.net/yanxiaolx/article/details/52282776),有了求二叉树的深度的经验之后再解决这个问题,我们很容易就能想到一个思路:在遍历树的每个结点的时候,调用函数TreeDepth得到它的左右子树的深度。如果每个结点的左右子树的深度相差都不超过1,按照定义它就是一棵平衡的二叉树。

代码实现:

// ====================方法1====================//求二叉树的深度int TreeDepth(BinaryTreeNode* pRoot){if (pRoot == NULL){return 0;}int nLeft = TreeDepth(pRoot->m_pLeft);int nRight = TreeDepth(pRoot->m_pRight);return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);}//需要重复遍历节点的算法,不够好bool IsBalanced_Solution1(BinaryTreeNode* pRoot){if (pRoot == NULL){return true;}int left = TreeDepth(pRoot->m_pLeft);int right = TreeDepth(pRoot->m_pRight);int diff = left - right;if (diff > 1 || diff < -1){return false;}return IsBalanced_Solution1(pRoot->m_pLeft) && IsBalanced_Solution1(pRoot->m_pRight);}


       上面的代码固然简洁,但我们也要注意到由于一个结点会被重复遍历多次,这种思路的时间效率不高。例如在IsBalancedBinaryTree方法中输入上图中的二叉树,我们将首先判断根结点(结点1)是不是平衡的。此时我们往函数TreeDepth输入左子树的根结点(结点2)时,需要遍历结点457。接下来判断以结点2为根结点的子树是不是平衡树的时候,仍然会遍历结点457。毫无疑问,重复遍历同一个结点会影响性能。

 

2)每个节点只需遍历一次的解法

 

  换个角度来思考,如果我们用后序遍历的方式遍历二叉树的每一个结点,在遍历到一个结点之前我们就已经遍历了它的左右子树。只要在遍历每个结点的时候记录它的深度(某一结点的深度等于它到叶节点的路径的长度),我们就可以一边遍历一边判断每个结点是不是平衡的。

代码实现:

// ====================方法2====================//后序每个节点只遍历一次,一边遍历一边记录它的深度bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth){if (pRoot == NULL){*pDepth = 0;return true;}int left, right;if (IsBalanced(pRoot->m_pLeft, &left) && IsBalanced(pRoot->m_pRight, &right)){int diff = left - right;if (diff <= 1 && diff >= -1){*pDepth = 1 + (left > right ? left : right);return true;}}return false;}bool  IsBalanced_Solution2(BinaryTreeNode* pRoot){int depth=0;return IsBalanced(pRoot, &depth);}

 

       在上面的代码中,我们用后序遍历的方式遍历整棵二叉树。在遍历某结点的左右子结点之后,我们可以根据它的左右子结点的深度判断它是不是平衡的,并得到当前结点的深度。当最后遍历到树的根结点的时候,也就判断了整棵二叉树是不是平衡二叉树。

 

完整代码及其测试用例实现:

 

#include<iostream>using namespace std;struct BinaryTreeNode{int             m_nValue;BinaryTreeNode*        m_pLeft;BinaryTreeNode*        m_pRight;};BinaryTreeNode* CreateBinaryTreeNode(int value){BinaryTreeNode* pNode = new BinaryTreeNode();pNode->m_nValue = value;pNode->m_pLeft = NULL;pNode->m_pRight = NULL;return pNode;}void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight){if (pParent != NULL){pParent->m_pLeft = pLeft;pParent->m_pRight = pRight;}}void DestroyTree(BinaryTreeNode* pRoot){if (pRoot != NULL){BinaryTreeNode* pLeft = pRoot->m_pLeft;BinaryTreeNode* pRight = pRoot->m_pRight;delete pRoot;pRoot = NULL;DestroyTree(pLeft);DestroyTree(pRight);}}// ====================方法1====================//求二叉树的深度int TreeDepth(BinaryTreeNode* pRoot){if (pRoot == NULL){return 0;}int nLeft = TreeDepth(pRoot->m_pLeft);int nRight = TreeDepth(pRoot->m_pRight);return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);}//需要重复遍历节点的算法,不够好bool IsBalanced_Solution1(BinaryTreeNode* pRoot){if (pRoot == NULL){return true;}int left = TreeDepth(pRoot->m_pLeft);int right = TreeDepth(pRoot->m_pRight);int diff = left - right;if (diff > 1 || diff < -1){return false;}return IsBalanced_Solution1(pRoot->m_pLeft) && IsBalanced_Solution1(pRoot->m_pRight);}// ====================方法2====================//后序每个节点只遍历一次,一边遍历一边记录它的深度bool IsBalanced(BinaryTreeNode* pRoot, int* pDepth){if (pRoot == NULL){*pDepth = 0;return true;}int left, right;if (IsBalanced(pRoot->m_pLeft, &left) && IsBalanced(pRoot->m_pRight, &right)){int diff = left - right;if (diff <= 1 && diff >= -1){*pDepth = 1 + (left > right ? left : right);return true;}}return false;}bool  IsBalanced_Solution2(BinaryTreeNode* pRoot){int depth=0;return IsBalanced(pRoot, &depth);}// ====================测试代码====================void Test(char* testName, BinaryTreeNode* pRoot, bool expected){if (testName != NULL){cout << testName<< " begins:" << endl;}cout << "Solution1 begins: " ;if (IsBalanced_Solution1(pRoot) == expected){cout << "Passed." << endl;}else{cout << "Failed." << endl;}cout << "Solution2 begins: ";if (IsBalanced_Solution2(pRoot) == expected){cout << "Passed." << endl;}else{cout << "Failed." << endl;}}void Test1(){// 完全二叉树//             1//         /      \//        2        3//       /\       / \//      4  5     6   7BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);ConnectTreeNodes(pNode1, pNode2, pNode3);ConnectTreeNodes(pNode2, pNode4, pNode5);ConnectTreeNodes(pNode3, pNode6, pNode7);Test("Test1", pNode1, true);DestroyTree(pNode1);}void Test2(){// 不是完全二叉树,但是平衡二叉树//             1//         /      \//        2        3//       /\         \//      4  5         6//        ///       7BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);ConnectTreeNodes(pNode1, pNode2, pNode3);ConnectTreeNodes(pNode2, pNode4, pNode5);ConnectTreeNodes(pNode3, NULL, pNode6);ConnectTreeNodes(pNode5, pNode7, NULL);Test("Test2", pNode1, true);DestroyTree(pNode1);}void Test3(){// 不是平衡二叉树//             1//         /      \//        2        3//       /\         //      4  5        //        ///       6BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);ConnectTreeNodes(pNode1, pNode2, pNode3);ConnectTreeNodes(pNode2, pNode4, pNode5);ConnectTreeNodes(pNode5, pNode6, NULL);Test("Test3", pNode1, false);DestroyTree(pNode1);}void Test4(){//               1//              ///             2//            ///           3//          ///         4//        ///       5BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);ConnectTreeNodes(pNode1, pNode2, NULL);ConnectTreeNodes(pNode2, pNode3, NULL);ConnectTreeNodes(pNode3, pNode4, NULL);ConnectTreeNodes(pNode4, pNode5, NULL);Test("Test4", pNode1, false);DestroyTree(pNode1);}void Test5(){// 1//  \//   2//    \//     3//      \//       4//        \//         5BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);ConnectTreeNodes(pNode1, NULL, pNode2);ConnectTreeNodes(pNode2, NULL, pNode3);ConnectTreeNodes(pNode3, NULL, pNode4);ConnectTreeNodes(pNode4, NULL, pNode5);Test("Test5", pNode1, false);DestroyTree(pNode1);}void Test6(){// 树中只有1个结点BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);Test("Test6", pNode1, true);DestroyTree(pNode1);}void Test7(){// 树中没有结点Test("Test7", NULL, true);}int main(){Test1();Test2();Test3();Test4();Test5();Test6();Test7();system("pause");return 0;}


运行结果:

Test1 begins:

Solution1 begins: Passed.

Solution2 begins: Passed.

Test2 begins:

Solution1 begins: Passed.

Solution2 begins: Passed.

Test3 begins:

Solution1 begins: Passed.

Solution2 begins: Passed.

Test4 begins:

Solution1 begins: Passed.

Solution2 begins: Passed.

Test5 begins:

Solution1 begins: Passed.

Solution2 begins: Passed.

Test6 begins:

Solution1 begins: Passed.

Solution2 begins: Passed.

Test7 begins:

Solution1 begins: Passed.

Solution2 begins: Passed.

请按任意键继续. . .

1 0
原创粉丝点击