Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.5 kB
1
Indexable
Never
Given a BST, return a copy of the tree with each node's value replaced with the sum of the values of all the nodes that are greater than that node. The resulting tree is not required to be a BST.

Example

input = {
    "value": 8,
    "left": {
      "value": 3,
      "left": {
        "value": 1,
        "left": null,
        "right": null
      },
      "right": {
        "value": 6,
        "left": {
          "value": 4,
          "left": null,
          "right": null
        },
        "right": {
          "value": 7,
          "left": null,
          "right": null
        }
      }  
    },
    "right": {
      "value": 10,
      "left": null,
      "right": {
        "value": 14,
        "left": {
          "value": 13,
          "left": null,
          "right": null
        },
        "right": null
      }
    }
}

the output should be:
{
    "value": 37,
    "left": {
      "value": 62,
      "left": {
        "value": 65,
        "left": null,
        "right": null
      },
      "right": {
        "value": 52,
        "left": {
         "value": 58,
         "left": null,
         "right": null
        },
        "right": {
         "value": 45,
         "left": null,
         "right": null
        }
      }  
    },
    "right": {
      "value": 27,
      "left": null,
      "right": {
        "value": 0,
        "left": {
         "value": 14,
         "left": null,
         "right": null
        },
        "right": null
      }
    }
}
Leave a Comment