[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 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
int sumOfTree(TreeNode* root)
{
if(root == NULL) return ;
if(root -> left == NULL && root -> right == NULL) return root->val;
return sumOfTree(root->left) + sumOfTree(root->right) + root->val;
}
int getTilt(TreeNode* root)
{
if(root == NULL || (root->left == NULL)&&(root->right == NULL)) return ;
return abs(sumOfTree(root->left) - sumOfTree(root->right) ) ;
}
int findTilt(TreeNode* root)
{
if(root == NULL || (root->left == NULL)&&(root->right == NULL)) return ; int left = findTilt(root->left);
int right = findTilt(root->right);
return left + right + getTilt(root);
}
[leetcode-563-Binary Tree Tilt]的更多相关文章
- 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 ...
- 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [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 ...
- 【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 ...
- 563. Binary Tree Tilt
https://leetcode.com/problems/binary-tree-tilt/description/ 挺好的一个题目,审题不清的话很容易做错.主要是tilt of whole tre ...
- 563. Binary Tree Tilt 子节点差的绝对值之和
[抄题]: Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as ...
- LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38
563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
随机推荐
- React-Router 4 的新玩意儿
上一个项目用的还是 2.6.1,转眼的功夫 4.0 都发布了,API 变化实在有点大,2.X那套东西不顶用了,老老实实重新看一遍文档,其中有几点需要注意的,拿出来说一说. 本文只讨论针对浏览器的应用, ...
- java面向对象--包及访问控制符
多人开发同一个项目时,会出现类名称相同的情况.package就是为了避免类或接口名称重复而采用的一种措施.实际上包就是有一定层次结构的文件夹,*.class文件要保存当前类声明的和包对应的文件夹中. ...
- TensorFlow for R
TensorFlow™ is an open source software library for numerical computation using data flow graphs. Nod ...
- ideal导入非maven工程-zdy
最近在老总给了一个ant部署的项目,发现ideal并没有支持ant直接一键自动配置的功能,like maven一样的,你们懂得.所以在这里记录一下我的心酸. 项目总体结构,webapp准备在ideal ...
- cas4.2以下取消https
deployerConfigContext.xml增加参数p:requireSecure="false" <bean class="org.jasig.cas.au ...
- Akka(3): Actor监管 - 细述BackoffSupervisor
在上一篇讨论中我们谈到了监管:在Akka中就是一种直属父子监管树结构,父级Actor负责处理直属子级Actor产生的异常.当时我们把BackoffSupervisor作为父子监管方式的其中一种.实际上 ...
- 玩转Storage Table 的PartitionKey,RowKey设计
参阅的文章 l https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/designing-a-scalable ...
- 如何优雅地实现Python通用多线程/进程并行模块
当单线程性能不足时,我们通常会使用多线程/多进程去加速运行.而这些代码往往多得令人绝望,需要考虑: 如何创建线程执行的函数? 如何收集结果?若希望结果从子线程返回主线程,则还要使用队列 如何取消执行? ...
- CentOS 虚拟机安装详解
第一步:安装 VMware 官方网站:www.vmware.com 下载百度云链接:http://pan.baidu.com/s/1bphDOWv 密码:0zix VMware 是一个虚拟 PC 的软 ...
- 一天搞定HTML----列表标签03
1.细说列表标签 2.代码演示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...