Wednesday, November 20, 2013

Find Max/Min Value in a Binary Tree (Not a Binary search tree)


//
//Recursive Max Value function
 int max_rec (Node *root)
{
    int left,right;   

    //Base Case
    if (root == NULL)
        return -1;

    left = max_rec(root->left);
    right = max_rec(root->right);

    return max (left , right , root->data) ;
}




//
//Iterative or Non Recursive Max using BFS Traversal
//

 int max_iter (Node *root)
{
    Node *temp = NULL;
    int tmp = -1 ;

    //if tree is null return
    if (!root)
        return 0;

    //Create a Queue and push root
    Que *Q = CreateQue();
    enQue(Q,root);

    //Loop while Queue is not empty
    while (!isEmpty(Q))
    {
        temp = deQue(Q);
        //check min data and save 
        if(tmp<temp->data)
            tmp = temp->data;
        if(temp->left)
            enQue(Q,temp->left);
        if(temp->right)
            enQue(Q,temp->right);
    }
    return tmp;
}



1 comment:

  1. The Casinos & Casino | Trick to Be the Best Casino Employee
    If you're looking for the best casinos in Las 웹툰사이트 Vegas, then you are 토토사이트 대여 샤오미 going to powerballgame co k be a 토토 중계 great choice 다 파벳 모바일 for your next job. Learn what's great and not

    ReplyDelete