剑指offer 39 - 二叉树的深度
2015-06-03 16:05
429 查看
1.先遍历二叉树的左子树的深度,然后再遍历二叉树右子树的深度。最后判断左子树和右子树的深度,如果左子树比右子树深则返回左子树深度+1,否则返回右子树深度+1。
2.判断该树是不是平衡树,我们用后序遍历的方式遍历整棵二叉树。在遍历某结点的左右子结点之后,我们可以根据它的左右子结点的深度判断它是不是平衡的,并得到当前结点的深度。当最后遍历到树的根结点的时候,也就判断了整棵二叉树是不是平衡二叉树了。
2.判断该树是不是平衡树,我们用后序遍历的方式遍历整棵二叉树。在遍历某结点的左右子结点之后,我们可以根据它的左右子结点的深度判断它是不是平衡的,并得到当前结点的深度。当最后遍历到树的根结点的时候,也就判断了整棵二叉树是不是平衡二叉树了。
#include <iostream> #include<vector> using namespace std; struct BinaryTreeNode { int m_nValue; BinaryTreeNode* m_pLeft; BinaryTreeNode* m_pRight; }; BinaryTreeNode* CreateBinaryTreeNode(int value) { BinaryTreeNode *head = new BinaryTreeNode[sizeof(BinaryTreeNode )]; head->m_nValue = value; head->m_pLeft = head->m_pRight = NULL; return head; } void ConnectTreeNodes(BinaryTreeNode* root ,BinaryTreeNode* left, BinaryTreeNode* right) { if(root != NULL) { root->m_pLeft = left; root->m_pRight = right; } } void DestroyTree(BinaryTreeNode* root) { if(root !=NULL) { BinaryTreeNode* left = root->m_pLeft; BinaryTreeNode* right= root->m_pRight; delete root; root=NULL; DestroyTree(left); DestroyTree( right); } } bool isBalanced(BinaryTreeNode* pRoot,int *depth) //节点深度作为参数,一边遍历一边判断是否平衡 { if(pRoot==NULL) { *depth=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) //此处不能写(-1<=diff<=1),错误 { *depth = left>right ? (left+1) :(right+1); return true; } } return false; } 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(BinaryTreeNode* pRoot) //方法2. 需要重复遍历节点多次 { if(pRoot== NULL) return true; int nLeftDepth = TreeDepth(pRoot->m_pLeft); int nRightDepth = TreeDepth(pRoot->m_pRight); int diff = nRightDepth-nLeftDepth; if (diff>1 || diff<-1) return false; return IsBalanced(pRoot->m_pLeft)&&IsBalanced(pRoot->m_pRight); } // ====================测试代码==================== void Test(char* testName, BinaryTreeNode* pRoot,bool result) { if(testName != NULL) printf("%s begins:", testName); int depth = 0; if(result == isBalanced(pRoot,&depth)) printf("%s\n","passed"); else printf("%s\n","failed"); printf("\n"); } // 1 // / \ // 2 3 // / \ \ // 4 5 6 // \ // 7 void Test1() { BinaryTreeNode* 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, NULL, pNode7); Test("Test1", pNode1,true); DestroyTree(pNode1); } // 1 // / // 2 // / // 3 // / // 4 // / // 5 void Test2() { BinaryTreeNode* 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("Test2",pNode1, false); DestroyTree(pNode1); } // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 void Test3() { BinaryTreeNode* 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("Test3",pNode1, false); DestroyTree(pNode1); } // 树中只有1个结点 void Test4() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); Test("test4",pNode1, true); DestroyTree(pNode1); } // 树中没有结点 void Test5() { Test("Test5",NULL,true); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); return 0; }
相关文章推荐
- HTML学习笔记之二(回到顶部 与 回到底部)
- C#后台找不到前台html标签
- Jquery判断是不是移动设备浏览
- JS实现自适应宽度的Tag切换
- html学习笔记(1)——html基本格式
- Nodejs学习笔记(四)与MySQL交互(felixge/node-mysql)
- 前端一些词汇的缩写
- js格式化时间戳
- JavaScript版本的策略模式
- .net 开源 JavaScript 解析引擎
- vultr vs digitalocean vs linode
- CSS设置水平居中和垂直居中
- local variable 'xxx' referenced before assignment
- JS setTimeout()学习笔记
- Html.RenderPartial、Html.RenderAction联系与区别
- jquery iframe自适应高度
- jquery的checked
- Chart.js报表制作
- JSON 数据列表取值
- 前端学习笔记之页面制作(一)——PS切图