平衡二叉树/镜像

来源:互联网 发布:windows不能切换到mac 编辑:程序博客网 时间:2024/05/20 23:56
#include<iostream>using namespace std;struct TreeNode {      int val;      struct TreeNode *left;      struct TreeNode *right;      TreeNode(int x)  :val(x)  ,left(NULL)  ,right(NULL)  {      }  };  bool _IsBalanceTree(TreeNode* root, int&  height,int& HD){//Height difference->HD高度差  if(root == NULL){  height=0;  HD=0;  return true;  }  //HD>0,左边高;HD<0,右边高。  int left= 0;  int leftHD= 0;  if(_IsBalanceTree( root->left, left, leftHD)){  //if(abs(leftHD)>1) return false;//左子树的  int right= 0;  int rightHD=0;  if(_IsBalanceTree( root->right, right, rightHD)){  //if(abs(rightHD)>1) return false;//右子树的高度差的绝对值不能大于1  if(abs(left-right)>0 && (abs(leftHD)+abs(rightHD))>1) return false;//左右子树的高度有差值,且左右子树高度差绝对值的和大于1  if(abs(HD)>1) return false;  if(left>right){  height=left+1;  HD = left-right;//计算差值  if(abs(HD)>1) return false;//高度差的绝对值不能大于1  return true;  }  else{  height=right+1;  HD = left-right;//计算差值  if(abs(HD)>1) return false;//高度差的绝对值不能大于1  return true;  }  }  return false;  }  return false;  }  //判断一棵二叉树是否是平衡二叉树  bool IsBalanceTree(TreeNode* root){//左右两个子树的高度差的绝对值不超过1->后序  int height=0;  int HD=0;  return _IsBalanceTree( root, height, HD);  }  void _Mirror(TreeNode* root){  if(root==NULL) return;  _Mirror(root->left);  _Mirror(root->right);  swap(root->left, root->right);  }  void PrintTree(TreeNode* root){//先序  if(root==NULL) return;  cout<<root->val<<" ";  PrintTree(root->left);  PrintTree(root->right);  }  //求一颗二叉树的镜像  TreeNode* Mirror(TreeNode* root){//求一颗二叉树的镜像->//后序遍历  TreeNode* tmp=root;  if(tmp==NULL)return tmp;  _Mirror(tmp);  return tmp;  }  void Test(TreeNode * pRoot1){//测试用例  cout<<"先序遍历: ";  PrintTree(pRoot1);//先序  cout<<"判断一棵二叉树是否是平衡二叉树: "<<IsBalanceTree(pRoot1)<<endl;//左右两个子树的高度差的绝对值不超过1->后序  cout<<"求一颗二叉树的镜像"<<endl;  TreeNode* tmp = Mirror(pRoot1);//求一颗二叉树的镜像->//后序遍历  cout<<"Mirror(pRoot1)->先序遍历: ";  PrintTree(tmp);//先序  tmp = Mirror(pRoot1);//为方便接下来的测试,复原原函数  }  void TestTree24(){//判断一棵二叉树是否是平衡二叉树+求一颗二叉树的镜像  TreeNode * pRoot1=new TreeNode(1);  TreeNode * pRoot2=new TreeNode(2);  TreeNode * pRoot3=new TreeNode(3);  TreeNode * pRoot4=new TreeNode(4);  TreeNode * pRoot5=new TreeNode(5);  TreeNode * pRoot6=new TreeNode(6);  TreeNode * pRoot7=new TreeNode(7);  TreeNode * pRoot8=new TreeNode(8);  TreeNode * pRoot9=new TreeNode(9);  cout<<"判断一棵二叉树是否是平衡二叉树+求一颗二叉树的镜像"<<endl;  cout<<"**********************************************"<<endl<<endl;  pRoot1->left = pRoot2;  pRoot2->left = pRoot4;  Test(pRoot1);//测试用例  cout<<endl<<endl;  pRoot4->left = pRoot8;  //  Test(pRoot1);//测试用例 //测试用例二屏蔽这三行即可。  cout<<endl<<endl;       //  pRoot1->right = pRoot3;  pRoot3->right = pRoot7;  Test(pRoot1);//测试用例  cout<<endl<<endl;  pRoot2->right = pRoot5;  Test(pRoot1);//测试用例  cout<<endl<<endl;  pRoot3->left = pRoot6;  Test(pRoot1);//测试用例  cout<<endl<<endl;  pRoot7->right = pRoot9;  Test(pRoot1);//测试用例  cout<<endl<<endl;  cout<<"**********************************************"<<endl;  }  int main(){  TestTree24();//判断一棵二叉树是否是平衡二叉树+求一颗二叉树的镜像  system("pause");  return 0;  }</span>  
阅读全文
0 0
原创粉丝点击