判断一棵二叉树是不是镜像对称的二叉树

来源:互联网 发布:淘宝客手机采集工具 编辑:程序博客网 时间:2024/04/29 14:28

 http://blog.csdn.net/spch2008/article/details/9365281

左右子树同时遍历,若出现不一致,则说明不对称。


代码如下:

[cpp] view plaincopyprint?
  1. struct Node  
  2. {  
  3.     Node *left;  
  4.     Node *right;  
  5.     Node()  
  6.     {  
  7.         left = right = NULL;  
  8.     };  
  9. };  
  10.   
  11. bool Judge(Node *leftRoot, Node *rightRoot)  
  12. {  
  13.     if(leftRoot != NULL && rightRoot != NULL)  
  14.     {  
  15.         if( Judge(leftRoot->left, rightRoot->left)  && Judge(leftRoot->right, rightRoot->right) )  
  16.             return true;  
  17.         else  
  18.             return false;  
  19.     }  
  20.     else if(leftRoot == NULL && rightRoot == NULL)  
  21.     {  
  22.         return true;  
  23.     }  
  24.     else  
  25.     {  
  26.         return false;  
  27.     }  
  28. }  
  29.   
  30.   
  31. bool JudgeTree(Node *root)  
  32. {  
  33.     if(root == NULL)  
  34.         return true;  
  35.   
  36.     return Judge(root->left, root->right);  
  37. }  

 

原创粉丝点击