https://leetcode.com/problems/binary-tree-tilt/description/

挺好的一个题目,审题不清的话很容易做错。主要是tilt of whole tree 的定义是sum of all node's tilt 而不是想当然的tilt of root.

一开是我就以为是简单的tilt of root 导致完全错误。思路其实可以看作是求sum of children 的变种,只是这里不仅要跟踪每个子树的sum 还要累计上他们的tilt。

/**
* 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 tiltIter(TreeNode* root, int *t) {
if (root == nullptr) {
return ;
} int leftSum = ;
int rightSum = ;
if (root->left != nullptr) {
leftSum = tiltIter(root->left, t);
}
if (root->right != nullptr) {
rightSum = tiltIter(root->right, t);
}
//tilt of the current node;
int tilt = abs(leftSum - rightSum);
*t += tilt;
return root->val + leftSum + rightSum;
} int findTilt(TreeNode* root) {
int tilt = ;
tiltIter(root, &tilt);
return tilt;
}
};

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

  1. 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 ...

  2. [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 ...

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

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

  4. 【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 ...

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

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

  6. LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38

    563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...

  7. [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 ...

  8. [Swift]LeetCode563. 二叉树的坡度 | 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 ...

  9. LeetCode Binary Tree Tilt

    原题链接在这里:https://leetcode.com/problems/binary-tree-tilt/description/ 题目: Given a binary tree, return ...

随机推荐

  1. (转)ReentrantLock实现原理及源码分析

    背景:ReetrantLock底层是基于AQS实现的(CAS+CHL),有公平和非公平两种区别. 这种底层机制,很有必要通过跟踪源码来进行分析. 参考 ReentrantLock实现原理及源码分析 源 ...

  2. 码云报错:fatal: remote origin already exists.解决方法

    今天在提交Git的时候,遇到了几个问题,记录一下,方便以后查找O(∩_∩)O 第一个问题 git remote add origin************** fatal: remote origi ...

  3. python实现查找文件

    import os.pathwhile True: rootdir=input('请输入遍历文件夹的绝对路径:(q退出)') if rootdir=='q': break if not(os.path ...

  4. 支付宝aar添加与友盟冲突解决

    Program type already present: com.ta.utdid2.b.a.e" 错误提示: 删掉libs中utdid的jar.

  5. socket(TCP)通讯之Python实现

    1.Service address = ('localhost', 9102) # AF_INET = ipv4; SOCK_STREAM:TCP s = socket.socket(socket.A ...

  6. python 生产者 --- 消费者

    值得拿出来 看看的 多进程 爬取 (生产) , 解析 (消费) 网页 同时进行,可以作为以后项目扩展使用 from bs4 import BeautifulSoup import requests i ...

  7. Kubernetes基本功能

    说明 目前kubernetes的资料介绍很多也很深刻,本文只是做一个针对自己学习k8s过程的介绍,仅仅是学习笔记的记录. 一.基本使用 1. 命令行 集群信息 Namespace 信息 Control ...

  8. file 多次上传附件功能完善

    之前解决了一个页面中的单个附件上传问题,使用的是 id 定位.但是一个页面中,可能存在多个附件上传的地方,这时候如果继续使用 id,会出问题. 我依旧会上传一个附件.附件链接地址: https://f ...

  9. 2019全国大学生信息安全竞赛部分Web writeup

    JustSoso 0x01 审查元素发现了提示,伪协议拿源码 /index.php?file=php://filter/read=convert.base64-encode/resource=inde ...

  10. [Reinforcement Learning] Cross-entropy Method

    Cross-entropy Method(简称CEM)虽然是一种基于交叉熵的算法,但并不是我们熟知的监督学习中的交叉熵方法,与其说它是一种基于交叉熵的算法,倒不如说是一种基于蒙特卡洛和进化策略的算法. ...