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 ...
随机推荐
- event driven model
http://www.jdon.com/eda.html http://blog.csdn.net/gykimo/article/details/9182287 事件代表过去发生的事件,事件既是技术架 ...
- HashTable的使用,扑克牌发牌游戏
l 场景 主要实现以下功能: 1. 首先给扑克牌中每张牌设定一个编号,下面算法实现的编号规则如下: 红桃按照从小到大依次为:1-13 方块按照从小到大依次为:14-26 黑桃按 ...
- 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境
搭建简单的测试环境 什么是响应首部 最简单的响应首部——Content-Type 设置响应首部 请求重定向与响应首部 在浏览器中查看Response Headers 1.先快速搭建一个简单的测试环境, ...
- java常用日期操作方法
package com.wujiangpo.test.util; import java.text.ParseException; import java.text.SimpleDateFormat; ...
- 七 、linux正则表达式
为处理大量的字符串而定义的一套规则和方法 1)linux正则表达式以行为单位处理 2)alians grep = “grep –color=auto”,让匹配的内容显示颜色 3)注意字符集,expor ...
- 《Inode与Block重要知识总结核心讲解》【转】
本文转载自:https://blog.csdn.net/BlackEnn/article/details/50787092 1.查看/dev/sda1下磁盘分区的block大小: 2.查看单个inod ...
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- 吴恩达深度学习笔记(九) —— FaceNet
主要内容: 一.FaceNet人脸识别简介 二.使用神经网络对人脸进行编码 三.代价函数triple loss 四.人脸库 五.人脸认证与人脸识别 一.FaceNet简介 1.FaceNet是一个深层 ...
- 服务器Windows 2008 R2 安装SQL 2008 R2
在站点下载 SQL 2008 R2 在安装数据库之前首先安装IIS和.NET 3.5 解压 找到运行程序 (这里需要修改路径,数据库一般不要安装在系统盘) (选择任何一个都可以,这里选择system ...
- 利用paramiko获取上传下载远程服务器的资源信息
# -*- coding: utf-8 -*- import paramikohostname='192.168.76.10'username='root'password='123456'param ...