1、题目描述

2、分析

利用递归实现。

3、代码

 int findTilt(TreeNode* root) {
if (root == NULL)
return ;
int ans = ;
nodesTilt(root,ans);
return ans;
} int nodesTilt(TreeNode *root, int & ans)
{
if (root == NULL)
return ;
int tiltleft = nodesTilt(root->left, ans);
int tiltright = nodesTilt(root->right, ans);
ans += abs(tiltleft - tiltright);
return tiltleft + tiltright + root->val;
}

LeetCode题解之Binary Tree Tilt的更多相关文章

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

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

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

  3. leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  4. [LeetCode 题解]: Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  5. leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...

  6. LeetCode算法题-Binary Tree Tilt(Java实现)

    这是悦乐书的第263次更新,第276篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第130题(顺位题号是563).给定二叉树,返回整棵树的倾斜度.树节点的倾斜被定义为所有 ...

  7. LeetCode题解之Binary Tree Right Side View

    1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...

  8. LeetCode题解之Binary Tree Pruning

    1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...

  9. LeetCode题解Maximum Binary Tree

    1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...

随机推荐

  1. Log4j最佳实践

    本文是结合项目中使用Log4j总结的最佳实践,非转载.网上可以找到的是这一篇<Log4j最佳实践>.本来Log4j使用是非常简单的,无需多介绍其用法,这只是在小型项目中:但在大型的项目中使 ...

  2. Silverlight中使用MVVM(4)—演练

    本来打算用MVVM实现CRUD操作的,这方面例子网上资源还挺多的,毕竟CRUD算是基本功了,因为最近已经开始学习Cailburn框架了,感觉时间 挺紧的,这篇就实现其中的更新操作吧.         ...

  3. Shell脚本 | 抓取log文件

    在安卓应用的测试过程中,遇到 Crash 或者 ANR 后,想必大家都会通过 adb logcat 命令来抓取日志定位问题.如果直接使用 logcat 命令的话,默认抓取出的 log 文件包含安卓运行 ...

  4. 修改linux的ssh默认端口号22的方法

    一.修改配置文件 vi /etc/ssh/sshd_config 找到#Port 22 修改为自己要使用的端口号:Port 26000 然后 :x  退出保存 二.重启ssh服务 /etc/init. ...

  5. paxos协议

    两将军问题 有两支军队,它们分别有一位将军领导,现在准备攻击一座修筑了防御工事的城市. 这两支军队都驻扎在那座城市的附近,分占一座山头. 一道山谷把两座山分隔开来,并且两位将军唯一的通信方式就是派各自 ...

  6. Normal Map中的值, Tangent Space, 求算 Tangent 与 Binormal 与 TBN Matrix

      - Normal Map中的值 -   有没有想过,Normal Map(法线贴图)为什么看上去都是“偏蓝色”的?这是因为,在map中存储的值都是在Tangent Space(切空间)下的.比如, ...

  7. VMware12 克隆虚拟机并且重新分配ip、mac

    记录如何快速拷贝一台虚拟机,并且重新分配IP.mac等方便自己做实验: 环境:VMWare12  .Redhat6.8 Step1.克隆虚拟机 虚拟机处于未打开状态,右击管理—>克隆 弹出克隆界 ...

  8. 【nginx笔记】系统参数设置-使Nginx支持更多并发请求的TCP网络参数

    首先,需要修改/etc/sysctl.conf来更改内核参数.例如,最常用的配置: fs.file-max = 999999 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tc ...

  9. PowerBuilder编程新思维4:钩挂(界面美化与DirectUI)

    <第二部分 Outside> PowerBuilder编程新思维4:钩挂(界面美化与DirectUI) PB的界面由于其封闭性,一直以来都是最大的弱项.自PB9.0开放了PBNI接口后,开 ...

  10. Netty 启动过程源码分析 (本文超长慎读)(基于4.1.23)

    前言 作为一个 Java 程序员,必须知道Java社区最强网络框架-------Netty,且必须看过源码,才能说是了解这个框架,否则都是无稽之谈.今天楼主不会讲什么理论和概念,而是使用debug 的 ...