[LeetCode&Python] Problem 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 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:
- The sum of node values in any subtree won't exceed the range of 32-bit integer.
- 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的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 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 ...
- 【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 ...
- 563. Binary Tree Tilt
https://leetcode.com/problems/binary-tree-tilt/description/ 挺好的一个题目,审题不清的话很容易做错.主要是tilt of whole tre ...
- 563. Binary Tree Tilt 子节点差的绝对值之和
[抄题]: Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as ...
- 【leetcode❤python】 257. Binary Tree Paths
深度优先搜索 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# se ...
- 【leetcode❤python】107. Binary Tree Level Order Traversal II
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
随机推荐
- Windows和Linux创建软链接和硬链接
1.Wondows创建软链接和硬链接 mklink [/d] [/h] link target /d--创建目录软链接:默认为文件软链接:创建目录链接时必须使用该选项不然创出的软链接无效 /h--创建 ...
- Win10怎么设置打开文件的默认程序
- swap分区不足ubuntu休眠
安装uswsusp Ubuntu gnu/linux只需 代码: sudo aptitude install uswsusp Arch gnu/linux系统 代码: sudo pacman -S u ...
- Win10系列:UWP界面布局基础3
在实际项目开发中,为控件属性赋值时经常会遇到属性值在设计时处于未知状态,而只有在应用程序运行时才能获取到.这种情况下,直接赋值方法是无法满足的,可以使用XAML标记扩展(Markup Extensio ...
- 微信公众号 access_token 没有过期 却失效
最近在开发微信项目的时候 access_token 缓存到 redis里面的,明明在两个小时的有效期内 微信却提示 "errcode":40001,"errmsg& ...
- linux 用户/群组/权限
mv 原文件名 新文件名 #相当于重命名 查看文件内容相关命令 cat #查看文件全部内容 head - n #查看文件前n行内容(默认前十行) tail -n #查看文件后n行内容(默认后十行) t ...
- 卷积与反卷积以及步长stride
1. 卷积与反卷积 如上图演示了卷积核反卷积的过程,定义输入矩阵为 I(4×4),卷积核为 K(3×3),输出矩阵为 O(2×2): 卷积的过程为:Conv(I,W)=O 反卷积的过称为:Deconv ...
- 四:(之三)制作镜像和一些docker命令
3.DIY image 3.1如何去掉sudo权限命令,让当前用户拥有操作docker的权限? 3.2 制作一个image: 拉取一个非常小的base image,hello-world.其中是一个可 ...
- [Linux]Linux下修改snmp协议的默认161端口
一.Linux SNMP的配置 SNMP的简介和Linux下IPV4,IPV6地址的snmp协议开启可以参考上一个随笔:[Linux]CentOS6.9开启snmp支持IPV4和IPV6 二.修改默认 ...
- 插入排序算法 Java实现
插入排序算法是算法排序中的一种: 该算法是假设已有序列是有序序列,从首元素(首元素为单个元素,肯定是有序的...)开始分析,对其他元素的位置进行有序的确定: 以算法为例: public class I ...