Given a binary search tree and two integers min and max, prune all nodes of binary search tree which are not in range min and max.
Key Point: post order traversal
If root is less than min or root is greater than max, we need to remove that node. However there is a catch here. What happens to its child nodes if root node is removed? To take care of this, we need to process children node before processing the root node.
Key Point: post order traversal
If root is less than min or root is greater than max, we need to remove that node. However there is a catch here. What happens to its child nodes if root node is removed? To take care of this, we need to process children node before processing the root node.
Read full article from Algorithms and Me: Prune nodes of binary search treeNode * prune_nodes(Node * root, int min, int max){if(root == NULL) return NULL;Node * root->left = prune_node(root->left, min, max);Node * root->right = prune_node(root->right, min, max);if(root->value < min)Node * right_tree = root->right;free(root);return right_tree;}if (root->value > max){Node *left_tree = root->left;free(root);return left_tree;}return root;}