LeetCode Binary Tree Tilt
原题链接在这里: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:
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.
题解:
用postorder traverse, 自下而上先算left的和,再算right的和, 取差的绝对值加进res中. postorder 返回 包括该点在内的和.
Time Complexity: O(n). n 是node个数.
Space: O(logn), stack space.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution { int res = 0; public int findTilt(TreeNode root) {
postorder(root);
return res;
} private int postorder(TreeNode root){
if(root == null){
return 0;
} int left = postorder(root.left);
int right = postorder(root.right);
res += Math.abs(left - right);
return left+right+root.val;
}
}
LeetCode Binary Tree Tilt的更多相关文章
- [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 ...
- LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38
563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- 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 ...
随机推荐
- level-8
CSS选择器常见的有几种? !important选择器——它是所有选择器里面权重最高的. *选择器——它是所有选择器里面权重最低的.可以为页面所有的标签添加统一样式,多用于页面整体样式调整,但它会增加 ...
- nodejs模块Phantom,无界面浏览器
PhantomJS 是一个无界面的 webkit 内核浏览器,
- pagination结合ajax
function getContent(page,Id){ $.ajax({ type:'get', url:'www.baidu.com', dataType:'jsonp', data:{ }, ...
- 类锁、对象锁、互斥锁与synchronized
本文总结自: https://blog.csdn.net/luckey_zh/article/details/53815694 互斥锁: 若对象有互斥锁,则在任一时刻,只能有一个线程访问对象.类锁.对 ...
- Golang html encoding解析
自动解析html页面的编码格式: 需要依赖 golang.org/x/text 和 golang.org/x/net 这两个外部库 package main import ( "net/ht ...
- 如何去掉Intellij IDEA过多的警告 设置警告级别
Intellij IDEA的代码提示系统很强大,根据严格的代码规范,包括简洁程度,运行效率,潜在bug提前发现等等给你做出了除编译器之外的大量额外提示.但这些提示有时会给我们带来困扰,比如弄的界面很乱 ...
- Java注解处理器
Java注解处理器 2015/03/03 | 分类: 基础技术 | 0 条评论 | 标签: 注解 分享到:1 译文出处: race604.com 原文出处:Hannes Dorfmann Java ...
- ML 线性回归Linear Regression
线性回归 Linear Regression MOOC机器学习课程学习笔记 1 单变量线性回归Linear Regression with One Variable 1.1 模型表达Model Rep ...
- spring boot 基础学习
构建微服务:Spring boot 入门篇 http://www.cnblogs.com/ityouknow/p/5662753.html SpringBoot入门系列:第一篇 Hello World ...
- boot小知识
lg 大, md 中等, sm 小, xs 极小. 可以单独用,也可以混合用,不同的屏幕用不同的比例. push ,pull 推拉.这个不实用. row里面可以嵌套实用row. 挤不下的时候,就会自动 ...