Untitled

Anonymous
plain_text
02/07/2024 4:28 PM
1.9 KB
18
Indexable
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
      }
    }
}
Editor is loading...
Leave a Comment