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.

题目标签:Tree

  这道题目给了我们一个二叉树,要我们求出二叉树的倾斜度。题目给了例子真的容易误导人。一开始以为只要求2个children的差值,其实是要求left 和 right 各自的总和,包括加上它们自己,left 和 right 两个总和值的差值。利用post order 遍历二叉树,post order 的顺序是 左 右 根。这样的话就可以求出根的sum,左和右都return了以后,加上它自己的值。但是这样的recursively call 每次返回的都是一个点的sum 总值。我们需要求的是差值的总和。所以需要另外设一个res, 每次把差值加入res。

Java Solution:

Runtime beats 92.40%

完成日期:06/30/2017

关键词:Tree

关键点:用post order去拿到每次left 和right 加上自己的总和;分清楚return的值并不是我们最终求的值,需要另外设置一个res,还有另一个help function

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
int res = 0; public int findTilt(TreeNode root)
{
postOrder(root); return res;
} public int postOrder(TreeNode node)
{
if(node == null)
return 0; int left_sum = postOrder(node.left); int right_sum = postOrder(node.right); res += Math.abs(left_sum - right_sum); return left_sum + right_sum + node.val;
}
}

参考资料:

https://leetcode.com/problems/binary-tree-tilt/#/solutions

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 563. Binary Tree Tilt (二叉树的倾斜度)的更多相关文章

  1. [LeetCode] 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 ...

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

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

  3. [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 ab ...

  4. 【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 ...

  5. [LeetCode] Invert Binary Tree 翻转二叉树

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...

  6. [LeetCode] Print Binary Tree 打印二叉树

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  7. 563. Binary Tree Tilt

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

  8. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

随机推荐

  1. eclipse:eclipse for java EE环境下如何配置tomcat服务器,并让tomcat服务器显示在控制台上,将Web应用部署到tomcat中

    eclipse环境下如何配置tomcat 打开Eclipse,单击"Window"菜单,选择下方的"Preferences".  单击"Server& ...

  2. MyBatis框架(一)

    MyBatis介绍: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...

  3. eastcom——eclipse中运行vtmserver项目

    1, vtmserver项目必须在tomcat7上运行. 2, 在Eclipse中vtmserver的截图 3, 在eclipse中配置一个tomcat7并将vtmserver加入其中 4, 在ecl ...

  4. [python学习笔记] String格式化

    格式化 S % (args...) 方式 特点 str里的占位符同java里的占位符. 优势 这种方式可以限定格式化的时候接受的数据类型. 常见占位符 %d 接收数字,格式化为 十进制 %x 接收数字 ...

  5. Jupyter(Python)中无法使用Cache原理分析

    前言 最近需要在Jupyter中写一个类库,其中有一个文件实现从数据库中读取空间数据并加载为Feature对象,Feature对象是cartopy封装的geomery列表,能够方便的用于作图等.因为有 ...

  6. SQLServer 自动循环归档分区数据脚本

    标签:SQL SERVER/MSSQL SERVER/数据库/DBA/表分区 概述 在很多业务场景下我们需要对一些记录量比较大的表进行分区,同时为了保证性能需要将一些旧的数据进行归档.在分区表很多的情 ...

  7. Python扩展方法一二事

    前言 跟着一个有强迫症的老板干活是一件极其幸福的事情(你懂的).最近碰到一个问题,简单的说就是对一个对象做出部分修改后仍然返回此对象,于是我就写了一个方法,老板看了之后只有一句话:不雅观,改成直接对此 ...

  8. MySQL数据备份之mysqldump使用(转)

    mysqldump常用于MySQL数据库逻辑备份. 1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump ...

  9. bzoj3713 [PA2014]Iloczyn|暴力(模拟)

    斐波那契数列的定义为:k=0或1时,F[k]=k:k>1时,F[k]=F[k-1]+F[k-2].数列的开头几项为0,1,1,2,3,5,8,13,21,34,55,-你的任务是判断给定的数字能 ...

  10. form表单中enctype="multipart/form-data"的传值问题

    form表单中enctype="multipart/form-data"的传值问题!! Form表单中enctype="multipart/form-data" ...