作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: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:

  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.

题目大意

找根节点左右子树的差值的和。

解题方法

Java解法

这个题很容易想到dfs,但是怎么写呢。这个用到的是后序遍历,用一个res来保存左右差的结果,函数的返回值是左右子树的和加上根节点的值。这样统计之后就能求出所有左右子树的和的差值。

  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. public class Solution {
  11. int res = 0;
  12. public int findTilt(TreeNode root) {
  13. postOrder(root);
  14. return res;
  15. }
  16. public int postOrder(TreeNode root){
  17. if(root == null){
  18. return 0;
  19. }
  20. int left = postOrder(root.left);
  21. int right = postOrder(root.right);
  22. res += Math.abs(left - right);
  23. return left + right + root.val;
  24. }
  25. }

Python解法

定义的后序遍历函数返回的值是左右子树的差。如果明白了这个定义之后就知道了,我们需要一个变量,在遍历的过程中求差的和。

  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution:
  8. def findTilt(self, root):
  9. """
  10. :type root: TreeNode
  11. :rtype: int
  12. """
  13. self.sums = 0
  14. self.postOrder(root)
  15. return self.sums
  16. def postOrder(self, root):
  17. if not root:
  18. return 0
  19. left = self.postOrder(root.left)
  20. right = self.postOrder(root.right)
  21. self.sums += abs(left - right)
  22. return left + right + root.val

日期

2017 年 5 月 9 日
2018 年 11 月 16 日 —— 又到周五了!

【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)的更多相关文章

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

  2. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

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

  3. 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...

  4. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

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

  5. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

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

  6. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  7. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  8. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  9. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

随机推荐

  1. iptables_超解

    查询: -t选项,指定要操作的表,使用-L选项,查看-t选项对应的表的规则,-L选项的意思是,列出规则,所以,上述命令的含义为列出filter表的所有规则 显示出了3条链INPUT链.FORWARD链 ...

  2. mac系统升级导致VirtualBox报Kernel driver not installed (rc=-1908)

    一.背景 最近将我的Mac升级成了Monterey版本,结果发现之前的安装的VirtualBox虚拟机无法启动,报了如下错误. Kernel driver not installed (rc=-190 ...

  3. HDFS01 概述

    HDFS 概述 目录 HDFS 概述 HDFS的产生背景和定义 HDFS产生背景 HDFS定义 优缺点 优点 缺点 组成 NameNode DataNode Secondary NameNode(2n ...

  4. Ecshop 安装

    参考 http://www.68ecshop.com/article-617.html ecshop的安装第一步:下载ecshop网店系统正式版安装包 我们可以来ecshop开发中心的官网(www.6 ...

  5. C++ 德才论

    输入样例: 14 60 80 10000001 64 90 10000002 90 60 10000011 85 80 10000003 85 80 10000004 80 85 10000005 8 ...

  6. Ruby Gems更换淘宝源方法

    官方的 Rubygems 源由于有些资源放在 Amazon S3 上面,所以有时会抽风,在 Linux 下我用 proxychains gem install xxx 实现了指定程序实行 Shadow ...

  7. 01_ubantu国内软件源配置

    查找自己版本对应的软件源 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 以下为19.10版本清华大学的,个人100M的带宽,平均安装速度在600K ...

  8. 【HarmonyOS】【多线程与并发】EventHandler

    EventHandler与EventRunner EventHandler相关概念 ● EventHandler是一种用户在当前线程上投递InnerEvent事件或者Runnable任务到异步线程上处 ...

  9. numpy基础教程--浅拷贝和深拷贝

    在numpy中,使用等号(=)直接赋值返回的是一个视图,属于浅拷贝:要完整的拷贝一个numpy.ndarray类型的数据的话,只能调用copy()函数 # coding = utf-8 import ...

  10. java多线程5:线程间的通信

    在多线程系统中,彼此之间的通信协作非常重要,下面来聊聊线程间通信的几种方式. wait/notify 想像一个场景,A.B两个线程操作一个共享List对象,A对List进行add操作,B线程等待Lis ...