https://leetcode.com/problems/binary-tree-tilt/description/
Very good a topic, examining is not clear words are easy to do wrong. The main tilt of whole tree is defined as the sum of all node ' s tilt instead of the tilt of root taken for granted.
I thought it was simple. Tilt of root causes a complete error. The idea can actually be seen as a variant of the sum of children, but not only to track the sum of each subtree but also to accumulate their tilt.
/** Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Public: intTiltiter (treenode* root,int*t) {if(Root = =nullptr) { return 0; } intLeftsum =0; intRightsum =0; if(Root->left! =nullptr) {Leftsum= Tiltiter (root->Left , T); } if(Root->right! =nullptr) {Rightsum= Tiltiter (root->Right , T); } //Tilt of the current node; intTilt = ABS (Leftsum-rightsum); *t + =tilt; returnRoot->val + leftsum +rightsum; } intFindtilt (treenode*root) { intTilt =0; Tiltiter (Root,&tilt); returntilt; }};
563. Binary Tree Tilt