---
# System prepended metadata

title: Untitled

---

![](https://i.imgur.com/LB3ufRK.png)
![](https://i.imgur.com/igXRg5U.png)

There is an edge with the cost between a node to node
Please find the min-cost from the root to leaf and return the leaf node
- [3, 5] => 8
- [3, 7] => 10
- [4, 6] => 10
- [5, 2] => 7 (min-cost)
- [5, 10] => 15


let ans = null;
let minCost = Number.MAX_VALUE;
const dp = {};
    
const dfs = (node, cost) => {
    if (dp[node])
    
    if db find node
        return db[node]
        
    if node > edge == 0
        return 0
        
        
    int minCost = INT_MAX;
    for (auto edge: eedges) {
    
        int currentCost = dfs(edge -> node) + edge.cost;
        
        //update hash
        
    
    }

    return minCost

    const currCost = node.val + cost;
    
    if (!node.minCost) {
        mode.minCost = currCost;
    }

    if (currCost > minCost) {
        return;
    }

    // bottom node
    if (!node.left && !node.right) {
        if (currCost < minCost) {
            minCost = currCost;
            ans = node;
        }

        return;
    }
    
    for (let i = 0; i < node.edges.length; i++) {
        const childNode = node.edges[i];
        
        if (childNode.minCost && childNode.minCost < currCost + childNode.val) {
            dfs(childNode, currCost);
        } else if (!childNode.minCost) {
            dfs(childNode, currCost);
        }
    }
}
    
const minCostLeftNode (root) => {
    dfs(root, 0);
    
    return ans;
}