sumOfLeftLeaves.js

    0

    0

    This code calculates the sum of the values in a list by traversing the list left to right, and returning the sum of the values in the leaf nodes. It initializes the sum to 0, then calls the traverse function on the list's root node to traverse the list left to right, and then calls the traverse function on the list's left and right nodes to determine if those nodes are left or right nodes and to add their values to the sum if they are.

    const sumOfLeftLeaves = (root) => {
        let sum = 0;
      
        const traverse = (node, isLeft) => {
          if (!node) return;
      
          if (!node.left && !node.right && isLeft) {
            sum += node.value;
          }
      
          traverse(node.left, true);
          traverse(node.right, false);
        };
      
        traverse(root, false);
      
        return sum;
      };
      
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.