Search
 
SCRIPT & CODE EXAMPLE
 

C

reverse binary tree c

void reverse_tree(tree_node_t* root) {
	
    if (root != NULL) {
    	
        // Reverse left and right sub-tree
        reverse_tree(root->left_child);
        reverse_tree(root->right_child);
        
        // Reverse two child nodes
        tree_node_t *temp = root->right_child;
        root->right_child = root->left_child;
        root->left_child  = temp;
        
	}

}
Comment

reverse binary tree c

// For embeded systems, dont use recursion, use stack (or queue) instead
// For more details check https://iq.opengenus.org/invert-binary-tree/

/*
typedef struct stack {
	stack_node_t* top;
} stack_t;

typedef struct stack_node {
	tree_node_t* t_node;	// Value of the stack node
    stack_node_t* next;
}stack_node_t;
*/

// What else you will likely need
stack_t* init_stack();
void stack_push();
tree_node_t* stack_pop();


void reverse_tree(tree_node_t* root) {
	
	if (root != NULL) {

      stack_t* stack = init_stack();	// Initialize stack
      tree_node_t* node, temp;	// Binary tree nodes


      while (stack->top != NULL) {
        
        // Dont forget to free the stack node inside the stack_pop()
		node = stack_pop(stack);

        temp = node->left_child;
        node->left_child = node->right_child;
        node->right_child = temp;

        if ( node->left_child != NULL ) stack_push (stack, node->left_child);

        if ( node->right_child != NULL ) stack_push (stack, node->rigth_child);


      }
		
      free(stack);
      
	}


}
Comment

reverse binary tree c

void reverse_tree(tree_node_t* root) {
	
    if (root != NULL) {
    	
        // Reverse left and right sub-tree
        reverse_tree(root->left_child);
        reverse_tree(root->right_child);
        
        // Reverse two child nodes
        tree_node_t *temp = root->right_child;
        root->right_child = root->left_child;
        root->left_child  = temp;
        
	}

}
Comment

reverse binary tree c

// For embeded systems, dont use recursion, use stack (or queue) instead
// For more details check https://iq.opengenus.org/invert-binary-tree/

/*
typedef struct stack {
	stack_node_t* top;
} stack_t;

typedef struct stack_node {
	tree_node_t* t_node;	// Value of the stack node
    stack_node_t* next;
}stack_node_t;
*/

// What else you will likely need
stack_t* init_stack();
void stack_push();
tree_node_t* stack_pop();


void reverse_tree(tree_node_t* root) {
	
	if (root != NULL) {

      stack_t* stack = init_stack();	// Initialize stack
      tree_node_t* node, temp;	// Binary tree nodes


      while (stack->top != NULL) {
        
        // Dont forget to free the stack node inside the stack_pop()
		node = stack_pop(stack);

        temp = node->left_child;
        node->left_child = node->right_child;
        node->right_child = temp;

        if ( node->left_child != NULL ) stack_push (stack, node->left_child);

        if ( node->right_child != NULL ) stack_push (stack, node->rigth_child);


      }
		
      free(stack);
      
	}


}
Comment

PREVIOUS NEXT
Code Example
C :: when to add & in snacf c 
C :: assembly to c code converter 
C :: C Common mistakes when working with pointers 
C :: c byte vs char 
C :: cut first part of string c 
C :: arcpy buffer 
C :: how we can strore a nested structure values in arrays 
C :: C program determines the height status for heights in cm 
C :: what to do after autoencoder training 
C :: panagram in c 
C :: c Modulo 10^9+7 (1000000007) 
C :: openinh VCL file for Vivado HLS 
C :: how to compress image in c 
C :: To get file info 
C :: gsl matrix invert 
C :: c programming trinary if 
C :: C what does /= mean 
C :: compile opencv program 
C :: #include <sys/time.h int main() { timespec ts; // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux } 
Dart :: flutter sleep 
Dart :: rel canonical tag 
Dart :: flutter text hint 
Dart :: raised button deprecated flutter 
Dart :: flutter list splice 
Dart :: refresh indicator flutter 
Dart :: increase text size of Test flutter 
Dart :: flutter compare dates 
Dart :: sort list descending from spesific maps key in dart 
Dart :: flutter container height 100 percent 
Dart :: convert a list to string in flutter 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =