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

Note:

  1. The sum of node values in any subtree won't exceed the range of 32-bit integer.
  2. All the tilt values won't exceed the range of 32-bit integer.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def findTilt(self, root):
"""
:type root: TreeNode
:rtype: int
"""
ans=[0]
def comT(root):
if not root:
return 0
left=comT(root.left)
right=comT(root.right)
ans.append(abs(left-right))
return left+right+root.val
if not root:
return 0
comT(root)
return sum(ans)

  

[LeetCode&Python] Problem 563. Binary Tree Tilt的更多相关文章

  1. [LeetCode&Python] Problem 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  2. [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  3. 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  4. LeetCode 563. Binary Tree Tilt (二叉树的倾斜度)

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  5. 【leetcode】563. Binary Tree Tilt

    Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...

  6. 563. Binary Tree Tilt

    https://leetcode.com/problems/binary-tree-tilt/description/ 挺好的一个题目,审题不清的话很容易做错.主要是tilt of whole tre ...

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

    [抄题]: Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as ...

  8. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  9. 【leetcode❤python】107. Binary Tree Level Order Traversal II

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

随机推荐

  1. Http Header Content-Typ

    Http Header里的Content-Type一般有这三种:application/x-www-form-urlencoded:数据被编码为名称/值对.这是标准的编码格式.multipart/fo ...

  2. Windows平台dump文件的产生,调试;工程配置pdb文件怎么生成

    http://blog.csdn.net/byxdaz/article/details/25872151

  3. php常见问题-foreach和引用造成的问题。

    结论:  foreach($arr as &$v) 类似这样的引用循环, 脚本语言需要注意,再次使用 $v时,他还指向原来的引用.会产生问题. unset($v)可以解除引用. 所以循环引用过 ...

  4. python常用

    列出当前目录 import os: print os.getcwd() python load .mat variable a import scipy.io as sp tmp=sp.loadmat ...

  5. nyoj-0613-免费馅饼(dp)

    nyoj-0613-免费馅饼 G. 免费馅饼 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在 ...

  6. mips编译器交叉编译openssl

    1.下载源码: git clone https://github.com/openssl/openssl.git 2. 配置生成Makefile ./config  no-asm shared --p ...

  7. TTL特殊门电路

    集电极开路(OC)门:主要作用实现线与功能:用做驱动器:实现电平转换 三态输出(TS)门:应用于计算机总线结构,通过分时控制三态门始轮端使得cpu与不同的外设通信:应用于双向传输,实现门电路与总线实现 ...

  8. xml解析与生成的学习资料

    xml解析与生成的学习资料:http://blog.csdn.net/u012325167/article/category/6129813 ----------------------------- ...

  9. 【转载】《Learn More Study Less》整体性学习方法

    原文 忘记在哪里看到这本书的介绍了,据说是一个小子自学1年,完成了4年麻省理工的课程,然后写了一本他学习方法的书.我搜了一下,居然中英文版都有,就花时间好好读了一遍,就是这本. 以下是这本书的完整笔记 ...

  10. java有关构造器的面试题详解

    1,编译器只会提供自动提供一个默认的无参数的构造函数 2,如果程序员没有给类A没有提供构造函数,则编译器会自动提供一个默认的无参数的构造函数,如果用户提供了自己的构造函数,则编译器就不在提供默认的无参 ...