Tilt of Binary Tree | Practice | GeeksforGeeks

Given a binary tree of size N+1, your task is to complete the function tiltTree(), that return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null nodes are assigned tilt to be zero. Therefore, tilt of the whole tree is defined as the sum of all nodes’ tilt.
Examples 1:
Input: 
       1
      / \
     2   3
Output: 1
Explanation:
Tilt of node 2 : 0
Tilt of node 3 : 0
Tilt of node 1 : |2-3| = 1
Tilt of binary tree : 0 + 0 + 1 = 1

Example 2:
Input:
        4 
       / \
      2   9 
     / \   \
    3   5   7 
Output: 15 
Explanation:
Tilt of node 3 : 0 
Tilt of node 5 : 0 
Tilt of node 7 : 0
Tilt of node 2 : |3-5| = 2 
Tilt of node 9 : |0-7| = 7 
Tilt of node 4 : |(3+5+2)-(9+7)| = 6 
Tilt of binary tree : 0 + 0 + 0 + 2 + 7 + 6 = 15

Your Task:
You don't need to read input or print anything. Your task is to complete the function tiltTree() which takes root node of the tree as input parameter and returns an integer denoting the tilt of the tree. If the tree is empty, return 0. 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=T<=100
0<=N<=500
 
0
rampurnaveen91 month ago
int solve(Node *root)   {       if(root==NULL)       {            return 0;       }       int s;       s = root->data + solve(root->left) +solve(root->right);       return s;   }   int check(Node *root,int &sum)   {       if(root == NULL)           return 0;                  sum = sum + abs(solve(root->left)-solve(root->right));       check(root->left,sum);       check(root->right,sum);   }   int tiltTree(Node *root)   {       int sum = 0;       check(root,sum);       return sum;   }
0
tarun2002ts0212 months ago
 int c(Node * root,int &ans){       if(!root) return NULL;              int lsum = c(root->left,ans);       int rsum = c(root->right,ans);              ans+=abs(lsum-rsum);       return root->data + lsum + rsum;          }   int tiltTree(Node *root)   {       int ans=0;       c(root,ans);       return ans;   }
+2
rdm1233 months ago
int hel(Node *root,int &a){       if(!root) return 0;       int l=hel(root->left,a);       int r=hel(root->right,a);       a+=abs(r-l);       return l+r+root->data;   }   int tiltTree(Node *root)   {       int a=0;       hel(root,a);       return a;       //code here   }
0
nekhatperveen3 months ago
JAVA
  public int tiltTree(Node root)   {       if(root==null)return 0;       int rootNode_tilt = nodetilt(root);       int tree_tilt= rootNode_tilt+tiltTree(root.left)+tiltTree(root.right);              return tree_tilt;          }   static int nodetilt(Node root)   {       if(root==null)return 0;       int node_tilt = Math.abs(sum_subtree(root.left)-sum_subtree(root.right));       return node_tilt;   }   static int sum_subtree(Node root)   {       if(root==null)return 0;       int sum = root.data+sum_subtree(root.left)+sum_subtree(root.right);       return sum;   }
+1
hydracody454 months ago
int ans=0;   int s(Node* root){       if(root==NULL){           return 0;       }       int x=s(root->left);       int y=s(root->right);       ans+=abs(x-y);       return root->data+x+y;   }   int tiltTree(Node *root)   {       //code here       s(root);       return ans;   }
0
kake13375 months ago
    int tilt(Node *root, int &s)   {       if(root==NULL)       return 0;       int x=tilt(root->left,s);       int y=tilt(root->right,s);       s+=abs(x-y);       return x+y+root->data;   }   int tiltTree(Node *root)   {       int tsum=0;      tilt(root,tsum);      return tsum;   }
+1
amitkumar3004206 months ago
class Solution{public:   // Your are required to complete this function   // function should return the tilt of the tree   int sumTree(Node *root)   {       if(root==NULL)       return 0;              return sumTree(root->left)+sumTree(root->right)+root->data;   }      int tiltTree(Node *root)   {   int sum=0;       if(root==NULL)       return 0;        sum +=abs(sumTree(root->left)-sumTree(root->right));       int l = tiltTree(root->left);       int r = tiltTree(root->right);                    return sum+r+l;              //code here   }};
0
Abhijit roy7 months ago
Abhijit roy
So, recursive function we will write like this...
Step1) If there is no parent, what the hell we are talking bout children then...Return 0
Step2) Make two children, indicating the two children of the dad(parent)...Now, trust in the recursion leap of faith and just recur to the left and right and store in their respective variables...We trust recursion and assume that both children(left and right child) have got their values...
Step3) Now we find the absolute difference of left and right values and store it.
Step4) Well...Now when each child is going to return the value he got to his dad....well he's gonna return his own two childrens+himself since he is also the child of the root...
int helper(Node* root, int &ans){                if(root==NULL){            return 0;        }        int leftsubtree=helper(root->left,ans);        int rightsubtree=helper(root->right,ans);        ans+= abs(leftsubtree-rightsubtree);        return root->data+rightsubtree+leftsubtree;    }    int tiltTree(Node *root) {        int ans=0;        helper(root,ans);        return ans;    }
https://media2.giphy.com/me...
0
Abhijit roy7 months ago
Abhijit roy
..
0
jasmeen kaur8 months ago
jasmeen kaur
 int c(Node * root,int &ans){        if(!root) return NULL;                int lsum = c(root->left,ans);        int rsum = c(root->right,ans);                ans+=abs(lsum-rsum);        return root->data + lsum + rsum;            }    int tiltTree(Node *root)    {        int ans=0;        c(root,ans);        return ans;    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.