Binary Tree Tilt – LeetCode 563
Problem
Description
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.
Answer
Original
Code
1 | /** |
思路
正常DFS。时间复杂度$O(n)$,空间复杂度$O(n)$。
耗时$8$ ms,排名$2.31\%$
Better
思路
还没看到更好的思路。