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

The sum of node values in any subtree won’t exceed the range of 32-bit integer.
All the tilt values won’t exceed the range of 32-bit integer.

思路:
见注释

代码:

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int findTilt(TreeNode* root) {help(root);//借用help函数来进行递归调用,因为返回值和findTilt不一样return tilt;}
private:int tilt=0;    //tilt为结果,初始化为0int help(TreeNode* root){if(root==NULL){//如果根节点为NULL,直接返回0return 0;}int left=help(root->left);//递归计算左子节点的和int right=help(root->right);//递归计算右子节点的和tilt+=abs(left-right);//计算当前节点的tilt值return root->val+left+right;}
};

输出结果: 16ms

LeetCode 563. Binary Tree Tilt相关推荐

  1. 563. Binary Tree Tilt 子节点差的绝对值之和

    [抄题]: Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as ...

  2. LeetCode: 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  3. LeetCode: 103. Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  4. LeetCode: Flatten Binary Tree to Linked List

    LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...

  5. 【重点】LeetCode 124. Binary Tree Maximum Path Sum

    LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...

  6. LeetCode 103. Binary Tree Zigzag Level Order Traversal

    LeetCode 103. Binary Tree Zigzag Level Order Traversal Solution1:基于层次遍历的微改 /*** Definition for a bin ...

  7. LeetCode 107. Binary Tree Level Order Traversal II

    LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码... /*** Definition f ...

  8. 【二叉树的迭代版后序遍历】LeetCode 145. Binary Tree Postorder Traversal

    LeetCode 145. Binary Tree Postorder Traversal Solution1:递归版答案 二叉树的后序遍历递归版是很简单的,关键是迭代版的代码既难理解又难写!但听了花 ...

  9. 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal

    LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...

最新文章

  1. SAP MM 如何通过SAP User ID拿到User的基本信息?
  2. 伸展树算法c语言,数据结构伸展树介绍及C语言的实现方法
  3. .CN域名总量达1090.6万个:8月份共净增13.8万个
  4. 【Linux 内核】进程管理 - 进程优先级 ② ( prio 调度优先级 | static_prio 静态优先级 | normal_prio 正常优先级 | rt_priority 实时优先级 )
  5. CV:利用cv2+dlib库自带frontal_face_detector(人脸征检测器)实现人脸检测与人脸标记之《极限男人帮》和《NBA全明星球员》
  6. C语言register关键字——最快的关键字
  7. 看了这个高并发系统架构,才知道我对秒杀的误解有多深
  8. STL训练 HDU - 1716 Ray又对数字的列产生了兴趣:
  9. opencv python 直方图反向投影_python OpenCV学习笔记直方图反向投影的实现
  10. linux下mycat测试安装
  11. 埋点用例管理_API管理平台之系统设计篇
  12. Nginx高可用实战
  13. hdu 5040 bfs
  14. 测试Live Writer Beta2功能
  15. adams 小车 平台 前行仿真
  16. android+桌面组件开发,Android桌面小部件开发,及注意事项
  17. ipad pro能用python编程吗_涨知识!iPad还可以运行Python代码
  18. 手把手带你学习Odoo OWL组件开发(1):认识 OWL
  19. 【Unity】问题记录:Some objects were not cleaned up when closing the scene
  20. 本科学计算机大学学金融工程,2020年金融工程专业排名

热门文章

  1. python单元测试mock_单元测试-具有多次调用方法的Python Mock对象
  2. 笨方法“学习python笔记之urlopen
  3. Numpy Math functions
  4. U-GAT-IT整体流程分析
  5. layui弹框提示层:倒计时(layui-font-red颜色定义)
  6. MongoDB可视化界面工具Studio3T的安装 - 讲解篇
  7. PHP二开 三语言( 中文、英语、马来语) 自动抢单系统源码
  8. ThinkCMF是一款支持Swoole的开源内容管理框架,基于ThinkPHP开发,同时支持PHP-FPM和Swoole双模式,让WEB开发更快!
  9. vue-cli3构建项目时elementUI按需引入问题
  10. 官方版.NET SDK连线更新(2011/01/19)