[英]replace each node of a tree with sum of all values in left side of the node without using extra integer pointer argument
Given a binary tree, I need to change the value in each node to sum of all the values in the nodes on the left side of the node . 给定一个二叉树,我需要更改每个节点中的值,以将节点左侧 所有节点中的所有值相加 。 Essentially each node should have the value equal to sum of all values of nodes visited earlier to this node in in-order traversal of the tree.
本质上,每个节点的值都应等于按树的顺序遍历此节点之前访问的所有节点的所有值的总和。 Important point is this has to be done without using integer pointer argument .
重要的一点是,必须在不使用整数指针参数的情况下完成此操作 。 I am able to solve it with interger pointer argument to hold sum like this.
我可以通过使用interger指针参数来解决它,以保持总和这样。 Without this integer pointer variable, How do I hold sum when I visit right side of a node from its parent.
如果没有此整数指针变量,当我从其父节点访问节点的右侧时,如何持有sum。
void modifyBST(struct node *root, int *sum) {
if (root == NULL) return;
// Recur for right subtree
modifyBSTUtil(root->left, sum);
// Now *sum has sum of nodes in right subtree, add
// root->data to sum and update root->data
*sum = *sum + root->data;
root->data = *sum;
// Recur for left subtree
modifyBSTUtil(root->right, sum);
}
How do I modify this method such that int *sum can be removed . 如何修改此方法,以便可以删除int * sum 。 My complete program is here click here
我的完整程序在这里点击这里
Example tree: Example tree: inorder: 4 2 5 1 6 3 7 preorder: 1 2 4 5 3 6 7 output : 4 6 11 12 18 21 28 示例树:示例树:顺序:4 2 5 1 6 3 7预定:1 2 4 5 3 6 7输出:4 6 11 12 18 21 28
int modify(struct node* node,int sum)
{
if (node == NULL)
return 0;
int l=modify(node->left,sum);
int r=modify(node->right,sum+l+node->data);
int x=node->data;
node->data=node->data+l+sum;
return x+l+r;
}
Call the function using statement: 使用语句调用该函数:
modify(root,0);
Full implementation : http://ideone.com/A3ezlk 全面实施: http : //ideone.com/A3ezlk
One possible solution: 一种可能的解决方案:
Rephrasing the question, what you really want to do is set each node's sum to the previous node's sum + the value associated with that node, ie 换个问题,您真正想要做的是将每个节点的总和设置为上一个节点的总和+与该节点关联的值,即
node n ->sum = node n-1 ->sum + node n ->value 节点n- >和=节点n-1- >和+节点n- >值
Right now, you are setting node n ->sum when you visit node n , but the issue you are running into is you don't have easy access to node n-1 ->sum without passing it as a parameter. 现在,当您访问节点n时,您正在设置节点n- > sum,但是遇到的问题是您无法轻松访问节点n-1- > sum而不将其作为参数传递。 To work around this, I would suggest setting node n ->sum when you DO have easy access to node n-1 ->sum, ie when you visit node n-1
要解决此问题,我建议在确实可以访问节点n-1- > sum时(即,当您访问节点n-1时)设置节点n- > sum
Some C code to show what I mean 一些C代码来说明我的意思
void modifyBST(struct node *curNode)
{
struct node* next = treeSuccessor(curNode);
if(next != NULL)
{
next->sum = curNode->sum + next->value;
modifyBST(next);
}
}
The assumption is that curNode->sum
has been set before modifyBST is called for that node, which is valid for the first node (as its sum is just equal to its value) and inductively valid for all other nodes. 假设在该节点调用ModifyBST之前已设置
curNode->sum
,该节点对第一个节点有效(因为其总和刚好等于其值),并且对所有其他节点都有效。
You would use this method by finding the first node (the one with value 4 in your example), if necessary setting sum equal to its value, and calling modifyBST with that node as the argument. 您将通过找到第一个节点(示例中值为4的节点)来使用此方法,必要时将sum设置为等于其值,然后使用该节点作为参数调用ModifyBST。
TreeSuccessor is a fairly well known algorithm, you can find pseudocode for it many places online if you need. TreeSuccessor是一个相当知名的算法,如果需要,您可以在许多地方在线找到它的伪代码。
I think below code should work just fine. 我认为下面的代码应该可以正常工作。 I have not tested it, just checked it manually.
我还没有测试,只是手动检查。
I hope this helps: 我希望这有帮助:
int modifyBST(struct node *root) {
if (root == NULL) return 0;
root->data += modifyBSTUtil(root->left);
return (root->data + modifyBSTUtil(root->right));
}
Since your pointer argument variable sum
never changes, we can easily get rid of it. 由于您的指针参数变量
sum
永远不会改变,因此我们可以轻松摆脱它。
int sum;
void modifyNode(struct node *root)
{
if (!root) return;
modifyNode(root->left);
sum = root->data += sum;
modifyNode(root->right);
}
void modifyTree(struct node *root)
{
sum = 0, modifyNode(root);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.