Given a binary tree, 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 node has tilt 0.
The tilt of the whole tree is defined as the sum of all nodes' tilt.
class Solution {//一个node的tilt 是全部左子节点和全部右子节点的差值
int result = 0;
public int findTilt(TreeNode root) {
postOrder(root);
return result;
}
public int postOrder(TreeNode root) {
if (root == null) {
return 0;
}
int left = postOrder(root.left);
int right = postOrder(root.right);
result += Math.abs(left - right);
return left + right + root.val;
}
}