原题链接在这里:https://leetcode.com/problems/binary-tree-tilt/description/

题目:

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:

  1. Input:
  2. 1
  3. / \
  4. 2 3
  5. Output: 1
  6. Explanation:
  7. Tilt of node 2 : 0
  8. Tilt of node 3 : 0
  9. Tilt of node 1 : |2-3| = 1
  10. 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.

题解:

用postorder traverse, 自下而上先算left的和,再算right的和, 取差的绝对值加进res中. postorder 返回 包括该点在内的和.

Time Complexity: O(n). n 是node个数.

Space: O(logn), stack space.

AC Java:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.  
  12. int res = 0;
  13.  
  14. public int findTilt(TreeNode root) {
  15. postorder(root);
  16. return res;
  17. }
  18.  
  19. private int postorder(TreeNode root){
  20. if(root == null){
  21. return 0;
  22. }
  23.  
  24. int left = postorder(root.left);
  25. int right = postorder(root.right);
  26. res += Math.abs(left - right);
  27. return left+right+root.val;
  28. }
  29. }

LeetCode 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) 38

    563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...

  3. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  4. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  5. 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 ...

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

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

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

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

  9. 563. Binary Tree Tilt

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

随机推荐

  1. 运维角度浅谈MySQL数据库优化

    一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善.这篇博文主要谈MySQL数据库发展周期中所面临的问题及优化方案,暂且抛开前端应用不说,大致分 ...

  2. PAT 天梯赛 L1-031. 到底是不是太胖了 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-031 AC代码 #include <iostream> #include <cstdio&g ...

  3. 使用git从本地上传至git码云远程仓库

    从 http://git-scm.com/download  下载window版的客户端.下载好,一步一步安装即可. 使用前的基本设置 git  config --global user.name & ...

  4. gstreamer——文档/资源/使用

    http://gstreamer.freedesktop.org/src/ http://gstreamer.freedesktop.org/data/doc/gstreamer/head/qt-gs ...

  5. Django基础知识MTV

    Django简介 Django是使用Python编写的一个开源Web框架.可以用它来快速搭建一个高性能的网站. Django也是一个MVC框架.但是在Django中,控制器接受用户输入的部分由框架自行 ...

  6. [NOI2008]设计路线

    题目 洛谷 BZOJ 做法 神仙题 显然这是棵树 个节点相东仅连接一个结点 不同于剖分,还能存在\("V"\)字型,一个节点最多与另外节点连两条边 \(dp[i][j][k]\)表 ...

  7. Windos Server 2008 NFS 服务安装使用

    系统环境:Windos 2008 R2 x64位 安装服务:NFS 文件服务 我的电脑-->右击管理-->功能-->添加功能 选择网络文件系统服务工具 安装服务 添加角色 下一步 选 ...

  8. for update排他锁详解

    使用场景: 高并发并且对于数据的准确性很有要求. 落实到mysql就是在事务中使用,只有使用InnoDB时才用,在begin于commit之间使用(只有此引擎支持事务). 本质: 给表或行上个锁以便接 ...

  9. Win32 API编程:WinMain无法重载函数或_tWinMain无法重载

    #include "windows.h" #include "tchar.h" int APIENTRY _tWinMain( HINSTANCE hInsta ...

  10. 出现GC overhead limit exceeded 的解决方案

    当我在使用MyEclispe IDE创建Maven项目的时候出现  "An internal error occurred during: “Build Project”. GC overh ...