+ Reply to Thread
Results 1 to 2 of 2

Thread: Given a node in a binary tree, find the next largest node.

  1. #1
    yuzi Guest

    Given a node in a binary tree, find the next largest node.

    We can do an inorder traversal as follows:
    Code:
    Node GetNextLargest(Node* node)
    {
    	if(NULL==node)
    		return NULL;
    	if(node->right) {
    		node=node->right;
    		if(node->left)
    			node=(node->left);
    	}
    	if(node->parent) {
    		if(node==node->parent->left)
    			return (node->parent);
    		else {
    			while(node->parent) {
    				if(node==node->parent->left)
    					return node->parent;
    				else 
    					node=node->parent;
    			}
    		}
    	}
    	return NULL;
    }

  2. #2
    jalajb2k7 Guest
    Code:
    void inorder(node *head,node *pointer){
         int count=0;
         node*p;
         if(node!=NULL){
                        inorder(node->left);
                        if(count==0){
                            p=node ;
                            count++;
                          }       
                        else{
                            if(p==pointer){
                                           return node;
                            }
                            else p=node;
                        
                                         
                        inorder(node->right);
         }
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find nth node from last
    By Surfer in forum Amazon
    Replies: 1
    Last Post: 29th April 2010, 16:01
  2. Find nth Node from end in a Linked List
    By TopGun in forum Linked Lists
    Replies: 0
    Last Post: 17th February 2008, 23:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts