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. python(4)之字典

    字典的操作方式如下: info={ 'stu1101':"xiaohai", 'stu1102':'liming', 'stu1103':"heima", } ...

  2. python 豆瓣验证码识别总结

    总结:  pytesseract 识别比较标准的图片  识别成功率   还是不错的. 验证码的图片识别 需要先处理好   再用pytesseract 识别 from PIL import Image  ...

  3. office install problems

    regedit 0000 "00005"或"00002"开头的项 remove all regedit options

  4. Mac使用crontab来实现定时任务

    crontab 定时执行 配置文件都在/etc/crontab下,如果没有就创建 语法: crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [ ...

  5. 怎么样才是设计功能函数的好思路(javascript)?

    在js里面,对于函数的调用,实际上也是也是面向对象的思路,于是写好js函数,也是考核面向对象设计的能力,同时也必须考虑到如何实现高内聚和低耦合,拿一个例子来说,现在的需求是这样的,实现个投资进度框,就 ...

  6. Win10系列:UWP界面布局进阶2

    为了让用户可以在流畅浏览应用界面的同时提供与应用相关的功能按钮,Windows 10系统在用户界面当中引入了侧边栏,侧边栏可以在用户有需要对应用或者系统进行操作时显示,在没有需要操作的时候自动隐藏,并 ...

  7. 微信公众号 access_token 没有过期 却失效

    最近在开发微信项目的时候 access_token   缓存到 redis里面的,明明在两个小时的有效期内  微信却提示 "errcode":40001,"errmsg& ...

  8. C++解析五-this 指针,指向类的指针

    C++this指针 在 C++ 中,每一个对象都能通过 this 指针来访问自己的地址.this 指针是所有成员函数的隐含参数.因此,在成员函数内部,它可以用来指向调用对象.友元函数没有 this 指 ...

  9. unity中实现三个Logo图片进行若隐若现的切换并有延时切换图片的效果

    public GameObject canvas; private Transform logoParent; private Transform Logo_logo; //logo一 private ...

  10. Mysql InnoDB三大特性-- change buffer

    Mysql InnoDB三大特性-- change buffer