TreeNode* helper(ListNode* head, int len) {
     if(len == 0) {
         return NULL;
     }
     if(len == 1) {
         return new TreeNode(head->val);
     }
     int value = nthNode(head, len/2)->val;
     TreeNode* root = new TreeNode(value);
     root->left = helper(head, len/2);
     root->right = helper(nthNode(head, len/2 + 1), (len-1)/2 );
     
     return root;
     
 }
Exemple #2
0
int main(void){
#if 0 
	Node *first=NULL;
	insert(3,&first);
	insert(5,&first);
	insert(1,&first);
	Node *second=NULL;
	insert(1,&second);
	insert(4,&second);
	insert(2,&second);
	display(&first);
	printf("\n");
	display(&second);
	printf("\n");
	addRev(&first,&second);
	// Node  *part = partitionPrint(&root,3);
	// printf("\n");
	// display(&part);
	
	//ktolast(&root,2);
#endif
	// A -> B -> C -> D -> E -> C
	Node *root =NULL;
	insert(1,&root);
	insert(2,&root);
	insert(3,&root);
	insert(4,&root);
	insert(5,&root);	
	insert(6,&root);
	display(&root);
//	reverse(&root);
	printf("\n");
	printf(" 2nd node from n is is %d \n",nthNode(2,&root));
	display(&root);
}