Given the root of a binary tree, return the sum of every tree node's tilt.

The tilt of a tree node is the absolute difference between the sum of all left subtree node values and all right subtree node values. If a node does not have a left child, then the sum of the left subtree node values is treated as 0. The rule is similar if there the node does not have a right child.

Example 1:

  1. Input: root = [1,2,3]
  2. Output: 1
  3. Explanation:
  4. Tilt of node 2 : |0-0| = 0 (no children)
  5. Tilt of node 3 : |0-0| = 0 (no children)
  6. Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3)
  7. Sum of every tilt : 0 + 0 + 1 = 1

Example 2:

  1. Input: root = [4,2,9,3,5,null,7]
  2. Output: 15
  3. Explanation:
  4. Tilt of node 3 : |0-0| = 0 (no children)
  5. Tilt of node 5 : |0-0| = 0 (no children)
  6. Tilt of node 7 : |0-0| = 0 (no children)
  7. Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5)
  8. Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7)
  9. Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16)
  10. Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15

这道题需要根据左右子树中节点的和来计算tilt,所以采用递归的方式,从底层往上计算,有个小trick就是对于最终求和的值利用传引用的方式更新数值。

  1. class Solution {
  2. public:
  3. int findTilt(TreeNode* root) {
  4. // 首先这就是一个简单的递归
  5. // 但是这个递归是从最底层开始计算 所以需要注意递归的顺序
  6. // 需要返回的值 当前节点加和的所有数值 求左右子树的所有节点的和
  7. //以及当前节点的倾斜值求和(用传引用减少变量)
  8. int res=0;
  9. int tmp=digui(root,res);
  10. return res;
  11. }
  12. int digui(TreeNode* node,int &res){
  13. if(node==nullptr) return 0;
  14. int left=digui(node->left,res);
  15. int right=digui(node->right,res);
  16. res+=abs(left-right);
  17. return (left+right+node->val);
  18. }
  19. };

【leetcode】563. Binary Tree Tilt的更多相关文章

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

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

  2. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  3. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  4. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  5. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  6. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

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

  7. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  8. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  9. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

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

随机推荐

  1. Vue面试题01

    说出vue常用的指令: v-text,  v-html,  v-bind,  v-for,  v-if,  v-else,  v-else-if,   v-show,    v-on, 谈谈你对MVC ...

  2. Mysql教程:(四)连接查询

    连接查询 1.左连接查询: mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu left join sco ...

  3. JDK源码阅读(5):HashTable类阅读笔记

    HashTable public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, ...

  4. 2020 天翼杯 部分wp

    天翼杯 呜呜呜呜 是我太菜了 Web APItest 源码 const express = require("express"); const cors = require(&qu ...

  5. Go语言核心36讲(Go语言实战与应用五)--学习笔记

    27 | 条件变量sync.Cond (上) 前导内容:条件变量与互斥锁 我们常常会把条件变量这个同步工具拿来与互斥锁一起讨论.实际上,条件变量是基于互斥锁的,它必须有互斥锁的支撑才能发挥作用. 条件 ...

  6. 网络带宽和速度测试windows和linux用iperf工具

    网络带宽和速度测试windows和linux用iperf工具   Iperf是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽,具有多种参数和UDP特性 ...

  7. JDBC操作多张表一

    一.操作一对多情况开发步骤1创建对象 //代码部门的对象public class Department { private String id; private String name; privat ...

  8. [cf741C]Arpa’s overnight party and Mehrdad’s silent entering

    直接令2i-1和2i的位置不相同,相当于有2n条边,对其进行二分图染色即可(这张图一定不存在奇环). 假设给出的n条关系是A类边,2i-1和2i的边是B类边,可以发现一条路径一定是AB交替(因为A/B ...

  9. [loj2850]无进位加法

    (似乎漏了一个数据范围,cf上的题面中还有$\sum L\le 3\cdot 10^{5}$) 考虑$a_{i}=2^{k_{i}}$时(不妨$k_{1}\ge k_{2}\ge ...\ge k_{ ...

  10. [atAGC048E]Strange Relation

    考虑对于$\{a_{i+1},...,a_{n}\}$,在其前面插入$a_{i}$对$x_{i}$的影响(不考虑$a_{1}$到$a_{i-1}$): 1.$x_{i}=0$,因为其前面没有数字了 2 ...