简体   繁体   中英

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. I have written the C++ code to find the min sum, but have problems in printing the path.

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?

Instead of 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM