判断二叉树是否平衡、是否完全二叉树、是否二叉排序树
来源:互联网 发布:知乎手机号已注册 编辑:程序博客网 时间:2024/05/16 14:52
http://blog.csdn.net/zhaojinjia/article/details/11891523
来自剑指offer
求树的深度
用递归做很简单,只要知道递归出口语句的别写错。
- struct BinaryTreeNode
- {
- int m_Value;
- BinaryTreeNode* m_pLeft;
- BinaryTreeNode* m_pRight;
- };
- int TreeDepth(BinaryTreeNode* pRoot)
- {
- if (pRoot == NULL)
- return 0;
- int nLeftDepth = TreeDepth(pRoot->m_pLeft);
- int nRightDepth = TreeDepth(pRoot->m_pRight);
- return (nLeftDepth>nRightDepth)?(nLeftDepth+1):(nRightDepth+1);
- }
判断该树是否为平衡二叉树
方法一:调用上述函数求每个节点的左右孩子深度
- bool IsBalanced(BinaryTreeNode* pRoot)
- {
- 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);
- }
方法二:由于上述方法在求该结点的的左右子树深度时遍历一遍树,再次判断子树的平衡性时又遍历一遍树结构,造成遍历多次。因此方法二是一边遍历树一边判断每个结点是否具有平衡性。
- bool IsBalanced(BinaryTreeNode* pRoot, int* depth)
- {
- if(pRoot== NULL)
- {
- *depth = 0;
- return true;
- }
- int nLeftDepth,nRightDepth;
- bool bLeft= IsBalanced(pRoot->m_pLeft, &nLeftDepth);
- bool bRight = IsBalanced(pRoot->m_pRight, &nRightDepth);
- if (bLeft && bRight)
- {
- int diff = nRightDepth-nLeftDepth;
- if (diff<=1 && diff>=-1)
- {
- *depth = 1+(nLeftDepth > nRightDepth ? nLeftDepth : nRightDepth);
- return true;
- }
- }
- return false;
- }
- bool IsBalanced(BinaryTreeNode* pRoot)
- {
- int depth = 0;
- return IsBalanced(pRoot, &depth);
- }
判断二叉树是否平衡、是否完全二叉树、是否二叉排序树
1.判断二叉树是否平衡
//求树的高度int TreeDepth(Node* t){ int hl,hr,h; if(t != NULL) { hl = TreeDepth(t->left); hr = TreeDepth(t->right); h = hl>hr? hl:hr; return h+1; } return 0;}//判断二叉树是否平衡int isBalanced(Node* t) { if(t==NULL) return 1; int leftDepth = TreeDepth(t->left); int rightDepth = TreeDepth(t->right); if(abs(leftDepth-rightDepth) > 1) return 0; else return isBalanced(t->left) && isBalanced(t->right); }
2.判断二叉树是否相同
//判断两棵二叉树是否相同int CompTree(Node* tree1, Node* tree2){ if(tree1 == NULL && tree2 == NULL) return 1; else if(tree1 == NULL || tree2 == NULL) return 0; if(tree1->data != tree2->data) return 0; if(CompTree(tree1->left,tree2->left)==1 && CompTree(tree1->right,tree2->right)==1) return 1; //反转二叉树也可能相同 if(CompTree(tree1->left,tree2->right)==1 && CompTree(tree1->right,tree2->left)==1) return 1; return 0;}
//拷贝二叉树 void CopyTree(Node* s,Node* & d) { if(s==NULL) d = NULL; return ; Node* temp = new Node; temp->data = s->data; if(d==NULL) d = temp; if(s->left) CopyTree(s->left,d->left); if(s->right) CopyTree(s->right,d->right); }
3.判断二叉树是否完全二叉树
判断二叉树是否是完全二叉树:层次遍历二叉树,遍历的左右节点入队列。若出队列的结点为空,则以后出队列的结点都为空,则为完全二叉树,否则不是
int ComplateTree(Node* bt){ Node* p=bt; queue<Node*> q; int tag=0; if(p==NULL) return 1; q.push(p); while(!q.empty()) { p=q.front(); q.pop(); if(p->left && !tag) q.push(p->left); else if(p->left) return 0; else tag=1; if(p->right && !tag) q.push(p->right); else if(p->right) return 0; else tag=1; } return 1;}
4.判断二叉树是否二叉排序树
判断二叉树是否是二叉排序树(BST):根据中序遍历序列是否升序来判断
bool IsBinarySortTree(Node* bt){ stack<Node*> s; Node* p = bt; Node* pre = NULL; // pre保持为p的中序前驱 while(p || !s.empty()) { if(p) { s.push(p); p = p->left; } else { p = s.top(); s.pop(); if( pre && (p->data <= pre->data) ) return false; // 不是二叉排序树 pre = p; // 记下前驱结点 p = p->right; } } return true; // 二叉排序树}
判断二叉树是否是二叉排序树(BST):层次遍历二叉树,若出队列的结点小于左结点的值,或者是大于右结点的值,则不是BST,否则是BST(感觉不对,BST要求左子树所有节点小于根节点,右子数所有节点大于跟节点,层次遍历出队只只比较左右节点而不是所有节点,应该不对!!!)
bool IsBST(Node* T){ queue<Node*> q; Node* p; if(T == NULL) return true; q.push(T); while(!q.empty()) { p = q.front(); q.pop(); if(p->left) if(p->data < p->left->data) return false; else q.push(p->left); if(p->right) if(p->data > p->right->data) return false; else q.push(p->right); } return true;}
作者:阿凡卢
出处:http://www.cnblogs.com/luxiaoxun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
0 0
- 判断二叉树是否平衡、是否完全二叉树、是否二叉排序树
- 判断二叉树是否平衡、是否完全二叉树、是否二叉排序树
- 判断二叉树是否平衡、是否完全二叉树、是否二叉排序树
- 判断二叉树是否平衡
- 判断二叉树是否平衡
- 判断二叉树是否平衡
- 判断二叉树是否平衡
- 判断二叉树是否平衡
- 判断二叉树是否平衡
- 判断二叉树是否平衡
- 判断二叉树是否平衡
- 判断二叉树是否平衡二叉树
- 判断平衡二叉树是否平衡
- 判断是否完全二叉树
- 判断某棵二叉树是否二叉排序树
- 判断二叉树是否为二叉排序树
- 判断二叉树是否为二叉排序树
- 判断二叉树是否为二叉排序树
- robots.txt 不让搜索引擎收录网站的方法
- MySQL从入门到精通(性能测试与调优)
- Fix “Unit iptables.service failed to load: No such file or directory” Error In CentOS7
- solr分组查询
- 解密90后为何最爱魅族 梦想校园行第三季启动
- 判断二叉树是否平衡、是否完全二叉树、是否二叉排序树
- android 图片转换
- android基础知识总结
- 强制转换横屏方法
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- Java 网络编程
- 使用reserve来避免不必要的重新分配
- 常用sqlite错误编码及获取文件路径方法
- JS 里面的 eval() 函数的作用和报错之后的处理