C solution


  • 1
    int tree_tilt = 0;
    int node_sum(struct TreeNode *root)
    {
    	int node_tilt,left_sum = 0, right_sum = 0;
    	if (root != NULL)
    	{
    		left_sum = node_sum(root->left);
    		right_sum = node_sum(root->right);
    		node_tilt = abs(left_sum - right_sum);
    		tree_tilt+=node_tilt;
    		return(root->val + left_sum + right_sum);
    	}
    	else
    		return(0);
    }
    int findTilt(struct TreeNode* root)
    {
        tree_tilt=0;
    	node_sum(root);
    	return(tree_tilt);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.