码迷,mamicode.com
首页 > 其他好文 > 详细

563. Binary Tree Tilt 子节点差的绝对值之和

时间:2018-03-17 22:06:43      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:tree node   画图   特殊   pre   des   输入   val   bin   example   

[抄题]:

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.

Example:

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

 [暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为要用hashmap把每个点的距离差都存起来,但其实用traverse的参数 就能实现自动记录

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

理解DFS的返回值适用于所有点,ans[0]的返回值只适用于root一个点 

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

563. Binary Tree Tilt 子节点差的绝对值之和

标签:tree node   画图   特殊   pre   des   输入   val   bin   example   

原文地址:https://www.cnblogs.com/immiao0319/p/8592958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!