[英]Min Path Sum in a Binary Tree
How to find the minimum path sum in a binary tree, and print the path? 如何在二叉树中找到最小路径总和并打印路径? The path can be from ROOT node to any LEAF node.
路径可以是从ROOT节点到任何LEAF节点的路径。 I have written the C++ code to find the min sum, but have problems in printing the path.
我已经编写了C ++代码来找到最小和,但是在打印路径时遇到了问题。
int MinTreePathSum(TreeNode *head, vector<TreeNode> &path)
{
if(!head) // head is NULL
return 0;
else if(!(head->left) && head->right) // only head->left is NULL
return head->val+MinTreePathSum(head->right, path);
else if(!(head->right) && head->left) // only head->right is NULL
return head->val+MinTreePathSum(head->left, path);
else
return head->val+min(MinTreePathSum(head->left, path), MinTreePathSum(head->right, path)); // none of left and right are NULL
}
The path
in the argument list is not used, could anyone help me to print the path which has the minimum path sum? 没有使用参数列表中的
path
,有人可以帮我打印路径总和最小的路径吗?
Instead of return head->val+min(MinTreePathSum(head->left, path), MinTreePathSum(head->right, path));
而不是
return head->val+min(MinTreePathSum(head->left, path), MinTreePathSum(head->right, path));
check which one of the right or left path is the minimum. 检查左右路径中的哪一条是最小的。 By saving them you can find the path.
通过保存它们,您可以找到路径。
int MinTreePathSum(TreeNode *head, string &path)
{
if(!head) // head is NULL
return 0;
else if(!(head->left) && head->right) // only head->left is NULL
{
string p;
int val = head->val+MinTreePathSum(head->right, p);
path = "R" + p;
return val;
}
else if(!(head->right) && head->left) // only head->right is NULL
{
string p;
int val = head->val+MinTreePathSum(head->left, p);
path = "L" + p;
return val;
}
else
{
int vl,vr,val;
string pl,pr;
vl = MinTreePathSum(head->left, pl);
vr = MinTreePathSum(head->right, pr);
if ( vl < vr ){
val = vl;
path = "L" + pl;
} else {
val = vr;
path = "R" + pr;
}
return head->val + val;
}
}
I think you are really close... just add the current node to path and choose shortest path if current node has two children: 我认为您真的很近...只要将当前节点添加到路径中,如果当前节点有两个子节点,则选择最短路径:
int MinTreePathSum(TreeNode *head, vector<TreeNode> &path)
{
if(!head) // head is NULL
return 0;
else if(!(head->left) && head->right) { // only head->left is NULL
path.push(*head);
return head->val+MinTreePathSum(head->right, path);
}else if(!(head->right) && head->left) { // only head->right is NULL
path.push(*head);
return head->val+MinTreePathSum(head->left, path);
}else{ // two children, must choose one...
path.push(*head);
// get left and right paths
pathLeft = vector<TreeNode>();
pathRight = vector<TreeNode>();
int valLeft = MinTreePathSum(head->left, pathLeft);
int valRight = MinTreePathSum(head->right, pathRight);
// actually copy the shortest path
if (valLeft < valRight) {
for(int i = 0; i < pathLeft.size(); ++i) {
path.push(pathLeft[i]);
}
}else{
for(int i = 0; i < pathRight.size(); ++i) {
path.push(pathRight[i]);
}
}
// finally return the minimum path, which is the one we put in "path" already
return head->val + min(valLeft, valRight);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.