https://practice.geeksforgeeks.org/problems/maximum-path-sum/1
this is question from geeks for geeks. i wrote my ans. but it is giving wrong. what is problem with my logic?
int path(Node *root, int & max_sum)
{
if(root==NULL)
return 0;
int l=path(root->left,max_sum);
int r=path(root->right,max_sum);
max_sum=max(max_sum,l+r+root->data);
return max(l,r)+root->data;
}
int maxPathSum(Node *root)
{
int max_sum=INT_MIN;
path(root,max_sum);
return max_sum;
// code here
}
The problem statement says
Find the maximum possible sum from one leaf node to another.
So, when you do
max_sum=max(max_sum,l+r+root->data);
You will need to check whether the root has both the children. Otherwise, they won't be considered in the final answer.
You also did
if(root==NULL) return 0;
and
max(l,r)+root->data;
These both combined are overriding a single leaf node child whose value is negative since 0 is greater than any negative integer.
So overall your code should look like this:
int path(Node *root, int & max_sum)
{
if(root==NULL) return 0;
int l=path(root->left,max_sum);
int r=path(root->right,max_sum);
if(root->left != NULL && root->right != NULL){
max_sum = max(max_sum,l + r + root->data);
}
if(root->left != NULL && root->right == NULL) return l + root->data;
if(root->right != NULL && root->left == NULL) return r + root->data;
return max(l,r) + root->data;
}
int maxPathSum(Node *root)
{
int max_sum=INT_MIN;
path(root,max_sum);
return max_sum;
}
Related
Could someone please let me know why my code isn't working? I am trying to count the number of times a given value appears in a binary tree using recursion. However, this approach is not working. I'd really appreciate some feedback and insight. Thank you.
public int valCount(int val) {
if (root == null) {
return 0;
}
return valCount(val, *root);
}
public int valCount(int val, Node *root) {
int cnt = 0;
if (root->left != null) {
if (root->data == val) {
cnt++;
}
int leftValCount = valCount(val, root->left);
}
if (root->right != null) {
if (root->data == val) {
cnt++;
}
int rightValCount = valCount(val, root->right);
}
return cnt + leftValCount + rightValCount;
}
A common mistake in recursion is to worry about stack frames other than the current one. Let the recursion do that work for you. Following this rule-of-thumb makes the code much easier to reason about.
The algorithm is simple: for each node, return 1 if the current node matches the target value else 0 if it doesn't and add the results of calling the same function on the left and right subtrees. The base case is when the function is called with a null root, return 0.
int valCount(int val, Node *root) {
if (!root) return 0;
return (root->val == val ? 1 : 0) +
valCount(val, root->left) +
valCount(val, root->right);
}
I am implementing an AVL tree and my search and insertion functions work properly, but I get a segmentation fault with my remove function. I have implemented a BST tree correctly before, so I know the issue is with the rebalancing of the tree rather than the initial deletion of a node.
Since my insertion operation works with the rebalancing, I also know the issue is not with the rotation functions themselves.
I have tried different strategies such as maintaining a balance factor at each node and have tried implementing other source code I have found online but I always end up with a segmentation fault and really cannot find where. I'd appreciate any help.
class AVL
{
public:
AVL();
Node* insert(int num);
Node* search(int num);
Node* remove(int num);
void print();
void comparisonPrint();
private:
int comparisonCount;
Node* root;
int max(int a, int b);
int getHeight(Node* t);
int getBalance(Node* t);
Node* insert(Node* &t, int num);
Node* rotateWithLeftChild(Node* t);
Node* rotateWithRightChild(Node* t);
Node* doubleRotateWithLeftChild(Node* t);
Node* doubleRotateWithRightChild(Node* t);
Node* search(Node* t, int num);
Node* removeMin(Node* parent, Node* node);
Node* remove(Node* &t, int num);
void print(Node* t);
//print
};
int AVL::max(int a, int b)
{
return (a > b)? a : b;
}
int AVL::getHeight(Node* t)
{
return (t == NULL) ? 0 : t->height;
}
int AVL::getBalance(Node* t)
{
if(t == NULL)
return 0;
return getHeight(t->leftChild) - getHeight(t->rightChild);
}
//helper function for remove - finds min
Node* AVL::removeMin(Node* parent, Node* node) //removes node, but does not delete - returns ptr instead
{
if(node != NULL)
{
if(node->leftChild != NULL) //go to leftmost child in right subtree
return removeMin(node, node->leftChild);
else //min val
{
parent->leftChild = node->rightChild;
return node;
}
}
else //subtree empty - incorrect use of function
return NULL;
}
Node* AVL::remove(Node* &t, int num)
{
cout << num;
if(t != NULL)
{
if(num > t->key)
{
comparisonCount++;
remove(t->rightChild, num);
}
else if(num < t->key)
{
comparisonCount++;
remove(t->leftChild, num);
}
else if(t->leftChild != NULL && t->rightChild != NULL)
{
comparisonCount++;
//2 children
Node* minRightSubtree = removeMin(t, t->rightChild);
t->key = minRightSubtree->key;
delete minRightSubtree;
}
else
{
comparisonCount++;
//0 or 1 child
Node* temp = t;
if(t->leftChild != NULL)
t = t->leftChild;
else if(t->rightChild != NULL)
t = t->rightChild;
delete temp;
}
//update height
t->height = max(getHeight(t->leftChild), getHeight(t->rightChild)) + 1;
int balance = getBalance(t);
if(balance > 1 && getBalance(t->leftChild) >= 0)
return rotateWithRightChild(t);
if(balance > 1 && getBalance(t->leftChild) < 0)
{
t->leftChild = rotateWithLeftChild(t->leftChild);
return rotateWithRightChild(t);
}
if(balance < -1 && getBalance(t->rightChild) <= 0)
return rotateWithLeftChild(t);
if(balance < -1 && getBalance(t->rightChild) > 0)
{
t->rightChild = rotateWithRightChild(t->rightChild);
return rotateWithLeftChild(t);
}
}
return t;
}
So I need the remove function to remove a specified node and rebalance the tree when necessary using the appropriate rotations. However, I keep getting a segmentation fault whenever I try to call the function in my main. Thanks.
There are two problems with your code. First is the removeMin function and the else if part in remove function when the node to be deleted has two children.
Basic aim of the removeMin function should be to find the inorder successor of the node to be deleted which is t in your case. Consider the case when t has 2 children (both leaf nodes) then your removeMin function will set t->leftChild as t->rightChild->rightChild which is NULL which is wrong. Also the restructuring of the tree should be done inside remove hence removeMin becomes:
Node* AVL::removeMin(Node* node) // returns inorder successor of 't'
{
if(node->left == NULL)
return node;
return removeMin(node->left);
}
Coming to remove function, we reset t->key with minRightSubtree->key and the node to be deleted now is minRightSubtree. But notice that the order of keys has changed in the chain from node t till node minRightSubtree. t->key is less than all the keys of nodes till before minRightSubtree. Hence you cannot just delete minRightSubtree, you have to call remove function on the node minRightSubtree which will take care of restructuring this chain. Also you can get a little help from the recursion stack to get the correct child for the current node t after deletion/rotation:
Node* AVL::remove(Node* &t, int num)
{
if (t == NULL)
return NULL;
if (num > t->key)
t->rightChild = remove(t->rightChild, num);
else if (num < t->key)
t->leftChild = remove(t->leftChild, num);
else if (t->leftChild != NULL && t->rightChild != NULL)
{
//2 children
Node* minRightSubtree = removeMin(t->rightChild);
t->key = minRightSubtree->key;
t->rightChild = remove(t->rightChild, minRightSubtree->key);
}
else
{
//0 or 1 child
Node* temp = t;
if (t->leftChild != NULL)
t = t->leftChild;
else if (t->rightChild != NULL)
t = t->rightChild;
if(temp == t)
t = NULL;
delete temp;
}
if (t == NULL) // this case was added since there is a possibility of deleting 't'
return NULL;
//update height
t->height = max(getHeight(t->leftChild), getHeight(t->rightChild)) + 1;
int balance = getBalance(t);
if (balance > 1 && getBalance(t->leftChild) >= 0)
return rotateWithRightChild(t);
if (balance > 1 && getBalance(t->leftChild) < 0)
{
t->leftChild = rotateWithLeftChild(t->leftChild);
return rotateWithRightChild(t);
}
if (balance < -1 && getBalance(t->rightChild) <= 0)
return rotateWithLeftChild(t);
if (balance < -1 && getBalance(t->rightChild) > 0)
{
t->rightChild = rotateWithRightChild(t->rightChild);
return rotateWithLeftChild(t);
}
return t;
}
I'm assuming your code for updating heights and balancing the rooted sub-tree is correct since I've forgotten about it's theory and will need to revise.
If I was asked about number of nodes in binary tree, it would be so easy but I am asked to count number of distinct nodes in binary tree like below.
There are two 12 values!
Number of nodes in binary tree algoritm is this:
struct Node {
string data;
struct Node *left;
struct Node *right;
};
int getNumberOfNodes(Node* node)
{
if (node != NULL)
return getNumberOfNodes(node->left) + 1 + getNumberOfNodes(node->right);
else
return 0;
}
But for unique values, it is too hard -_-
You can change your function adding a container to maintain the values you already encountered. The best container has been suggested in the comment std::set.
The new code would be:
int getNumberOfNodes(Node* node, std::set<string>& uniqueValues)
{
if (node != NULL)
{
int count = 0;
if ( uniqueValues.find( node->data ) == uniqueValues.end() )
{
count = 1;
uniqueValues.insert ( node->data );
}
return getNumberOfNodes(node->left,uniqueValues) + count + getNumberOfNodes(node->right,uniqueValues);
}
else
return 0;
}
Not so different from your code.
At the end the uniqueValues.size() will be equal to the returned int.
Clear the uniqueValues before calling the function.
int count_label(Node *root, int data)
{
int count_of_data = 0;
if(root == NULL)
return 0;
if(data == root->data)
count_of_data += 1;
if(data > root->data)
count_of_data += count_label(root->right,data);
else
count_of_data += count_label(root->left,data);
return count_of_data;
}
//--------------------------------------------------------
unsigned int unique_nodes(Node *root)
{
int count_u = 0;
if(root == NULL)
return 0;
if(count_label(root, root->data) == 1)
{
count_u += 1;
}
count_u += unique_nodes(root->left);
count_u += unique_nodes(root->right);
return count_u;
}
So I tried my own solution in C++ but there is a bug in the code. That problem comes from judge.
So what I'm doing is keep adding a sum value and then check if the provided sum equals to the total sum in a leaf.
bool hasPathSum(TreeNode *root, int sum) {
stack<TreeNode*> st;
TreeNode *temp = root;
int SUM = 0;
bool hasSum = false;
st.push(temp);
while(!st.empty() && temp != NULL)
{
if(temp)
{
st.push(temp);
temp = temp->left;
}
else
{
st.pop();
temp = st.top();
SUM += temp->val;
if(SUM == sum)
hasSum = true;
temp = temp->right;
}
}
return hasSum;
}
Trivial to express recursively:
bool hasPathSum(TreeNode *node, int sum) {
if (!node) {
return sum == 0;
}
return hasPathSum(node->left, sum-node->val) ||
hasPathSum(node->right, sum-node->val);
}
If you translate this to a stack implementation, you will see some of the problems in yours. In particular, it is only at the leaves you want to check the sum (you check interior nodes). You have to adjust the sum as you go up and down the tree (you always add to it).
public static boolean hasPathSum(TreeNode node, int targetSum) {
if (node == null) return false;
targetSum-= node.val;
if (targetSum == 0 && node.left==null && node.right==null) {
return true;
}
int left = hasPathSum(node.left, targetSum);
int right = hasPathSum(node.right, targetSum;
return left || right;
}
This is how my code looks
int main() {
//root is the rootnode of the tree
if(root!==NULL) {
int mini = min(root, root->data);
printf("minimum number is %d", mini);
}
return 0;
}
int min(node *root, int mini) {
if(root == NULL) {
return;
}
min(root->left, mini);
min(root->right, mini);
if(mini > root->data) {
mini = root->data;
}
return mini;
}
It doesn't give me the minimum number in the tree. Rather, it prints the root node as the minimum.
You need to use the results of the recursive calls instead of throwing them away.
They are supposed to be the minimum values of the subtrees.
The minimum value in a tree is the minimum of
The root value
The value in the left subtree, if there is one
The value in the right subtree, if there is one
Like this:
int min(node *root)
{
int least = root->data;
if (root->left != NULL)
{
least = std::min(least, min(root->left));
}
if (root->right != NULL)
{
least = std::min(least, min(root->right));
}
return least;
}
Try this:
int min(node *root, int mini) {
if(root == NULL) {
return -1;
}
int a;
a=min(root->left, mini);
if(a<mini)
mini=a;
a=min(root->right, mini);
if(a<mini)
mini=a;
if(root->data<mini)
mini = root->data;
return mini;
}
This works for me. Not discarding the return value.
int main() {
//root is the rootnode of the tree
if(root!==NULL) {
int mini = min(root, root->data);
printf("minimum number is %d", mini);
}
return 0;
}
int min(node *root, int mini) {
if(root == NULL) {
return;
}
mini = min(root->left, mini);
mini = min(root->right, mini);
if(mini > root->data) {
mini = root->data;
}
return mini;
}