码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树深度、平衡、相等、求遍历

时间:2014-09-03 00:08:45      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   2014   

#include <iostream>   
#include <fstream>   
#include <string>   
using namespace std;
struct TreeNode  
{  
    struct TreeNode* left;  
    struct TreeNode* right;  
    char  elem;  
};  

//知道先序遍历和中序遍历 求后序遍历
TreeNode* BinaryTreeFromOrderings(char* inorder, char* preorder, int length)  
{  
    if(length <= 0||preorder==NULL||preorder==NULL)//递归终止条件  
    {  
        return NULL;  
    }  
    TreeNode* node = new TreeNode;//Noice that [new] should be written out.   
    node->elem = *preorder; //将根节点赋值给新节点 
    int rootIndex = 0;  
    for(;rootIndex < length; rootIndex++)//找出中序遍历中的头结点 
    {  
        if(inorder[rootIndex] == *preorder)  
            break;  
    } 
    if (rootIndex>=length)//如果没有找到 
    {
        throw std::exception("invalid input");
    }
    node->left = BinaryTreeFromOrderings(inorder, preorder +1, rootIndex);//左子树递归  
    node->right = BinaryTreeFromOrderings(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1));//右子树递归
    std::cout<<node->elem;  
    return node;  
}  


//递归求二叉树的深度
int PostTreeDepth(TreeNode *root)
{
    int max,leftheight,rightheight;
    if (root==NULL)
    {
        return 0;
    }
    else
    {
        leftheight=PostTreeDepth(root->left);
        rightheight=PostTreeDepth(root->right);
        max=leftheight>rightheight?leftheight:rightheight;
        return (max+1);
    }
}
//判断两个树是否相等
bool isTwoTreesEuqal(TreeNode *tree1,TreeNode *tree2)
{
    if (tree1==NULL&&tree2==NULL)//两个都空 相等
    {
        return true;
    }
    if (tree1&&!tree2||!tree1&&tree2)//一个空一个非空 不等
    {
        return false;
    }
    if (tree1&&tree2)//两个都非空 判断是否相等
    {
        if (tree1->elem==tree2->elem)
        {
            if (isTwoTreesEuqal(tree1->left,tree2->left))
            {
                return isTwoTreesEuqal(tree1->right,tree2->right);
            }
            else if (isTwoTreesEuqal(tree1->left,tree2->right))
            {
                return isTwoTreesEuqal(tree1->right,tree2->left);
            }
            
        }
    }
    return false;
}


//判断二叉树是否是平衡二叉树
bool isBalanced(TreeNode *root)
{
    if (root==NULL)
    {
        return true;
    }
    int leftheight=PostTreeDepth(root->left);
    int rightheight=PostTreeDepth(root->right);
    if (abs(leftheight-rightheight)>1)//如果大于1的话直接返回假 否则继续递归判断
    {
        return false;
    }
    else
        return isBalanced(root->left)&&isBalanced(root->right);
}
int main(int argc, char** argv)
{  
    char* pr="GDAFEMHZ";      
    char* in="ADEFGHMZ"; 
    TreeNode *root1=BinaryTreeFromOrderings(in, pr, 8); 
    cout<<endl;
    cout<<PostTreeDepth(root1)<<endl;
    char* pr2="DAFEMHZ";      
    char* in2="ADEFHMZ"; 
    TreeNode *root2=BinaryTreeFromOrderings(in2, pr2,7);
    cout<<endl;
    cout<<PostTreeDepth(root1)<<endl;
    cout<<isTwoTreesEuqal(root1,root2)<<endl;
    cout<<isBalanced(root1)<<endl;
    return 0;
} 

bubuko.com,布布扣

二叉树深度、平衡、相等、求遍历

标签:style   blog   http   color   os   io   ar   for   2014   

原文地址:http://www.cnblogs.com/mu-tou-man/p/3952444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!