563. 二叉树的坡度

563. Binary Tree Tilt

题目描述

给定一个二叉树,计算整个树的坡度。

一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值。空结点的的坡度是 0。

整个树的坡度就是其所有节点的坡度之和。

每日一算法2019/6/10Day 38LeetCode563. Binary Tree Tilt

示例:

输入:

     1
/ \
2 3

输出: 1

解释:

结点的坡度 2 : 0

结点的坡度 3 : 0

结点的坡度 1 : |2-3| = 1

树的坡度 : 0 + 0 + 1 = 1

注意:

  1. 任何子树的结点的和不会超过 32 位整数的范围。
  2. 坡度的值不会超过 32 位整数的范围。

Java 实现

TreeNode Class

public class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}
class Solution {
public int findTilt(TreeNode root) {
if (root == null) {
return 0;
}
int[] res = new int[1];
postorder(root, res);
return res[0];
} public int postorder(TreeNode root, int[] res) {
if (root == null) {
return 0;
}
int leftSum = postorder(root.left, res);
int rightSum = postorder(root.right, res);
res[0] += Math.abs(leftSum - rightSum);
return leftSum + rightSum + root.val;
}
}

参考资料

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

  1. [Swift]LeetCode563. 二叉树的坡度 | 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. Java实现 LeetCode 563 二叉树的坡度(又是一个遍历树)

    563. 二叉树的坡度 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点的的坡度是0. 整个树的坡度就是其所有节点的坡度之和. ...

  3. [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

    [题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  6. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  7. Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)

    Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...

  8. 笔试算法题(41):线索二叉树(Threaded Binary Tree)

    议题:线索二叉树(Threaded Binary Tree) 分析: 为除第一个节点外的每个节点添加一个指向其前驱节点的指针,为除最后一个节点外的每个节点添加一个指向其后续节点的指针,通过这些额外的指 ...

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

随机推荐

  1. EntityFramework6 学习笔记(一)

    1.什么是EF? EF是一种ORM(Object-relational mapping)框架,它能把我们在编程时使用对象映射到底层的数据库结构.比如,你可以在数据库中建立一个Order表,让它与程序中 ...

  2. 小a的学期 (组合数取模模板)

    题目描述 小a是一个健忘的人,由于他经常忘记做作业,因此老师对他很恼火. 小a马上就要开学了,他学期一共2n 天,对于第i天,他有可能写了作业,也可能没写作业,不过他自己心里还有点B数,因此他会写恰好 ...

  3. H5利用formData来上传文件(包括图片,doc,pdf等各种格式)方法小结!

    H5页面中我们常需要进行文件上传,那么怎么来实现这个功能呢??? 我主要谈如下两种方法. (一).传统的form表单方法 <form action="/Home/SaveFile1&q ...

  4. learning java 实例序列化

    对Person类实例进行序例化及反序例化: Person.java public class Person implements java.io.Serializable { private Stri ...

  5. 2019.12.11 java练习

    class Demo01 { public static void main(String[] args) { //数组求最大值 int[] arr={1,2,3,4,5,6,7,8,9}; int ...

  6. hibernateHQL语句

    一.hql 1. 什么是hql HQL是Hibernate Query Language的缩写 查全部 2. hql和sql区别/异同 HQL SQL 类名/属性 表名/列名 区分大小写,关键字不区分 ...

  7. 一篇来自hasura graphql-engine 百万级别live query 的实践

    转自:https://github.com/hasura/graphql-engine/blob/master/architecture/live-queries.md Scaling to 1 mi ...

  8. gj的zabbix客户端开机自启动设置

    查看是否自启动 配置chkconfig 启动服务

  9. P2388 阶乘之乘

    首先感谢wxy学长之前告诉我这道题,结果今天竟然一眼切了,咕咕咕 题目链接: P2388 阶乘之乘 题目思路: 第一眼看到一定想到的是先求一下阶乘然后看最后又几个零,但是这样会TIL啊 想一下0是怎么 ...

  10. P4899 【[IOI2018] werewolf 狼人】

    感觉已经几次碰到这种类型的题目了,写篇\(Blog\)总结一下 题意: 是否存在一条\((s_i, t_i)\)的路径,满足先只走编号不超过\(L_i\)的点,再走编号不超过\(R_i\)的点 \(S ...