Children Sum Property

Dhanaraj S
Apr 17, 2022

--

Check the problem description here.

https://www.codingninjas.com/codestudio/problems/childrensumproperty_790723?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website&leftPanelTab=0

Solution:

As we recursively go to left and right , we replace child data , with root data, if the root is less than the root , so that while we come back the child of a node is always higher than the root. So that we can increment the root value(make it left +right data).

def changeTree(root): 
if root ==None:
return
left=0
right=0
if root.left!=None:
if root.left.data<root.data:
root.left.data=root.data
changeTree(root.left)
left=root.left.data
if root.right!=None:
if root.right.data<root.data:
root.right.data=root.data
changeTree(root.right)
right=root.right.data
if not (root.left==None and root.right==None):
root.data=left+right

Time:O(N)

Space:O(h)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Dhanaraj S
Dhanaraj S

Written by Dhanaraj S

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

No responses yet

Write a response