leetcode563
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
/// <summary>
/// 遍历二叉树,存储节点列表
/// </summary>
Stack<TreeNode> S = new Stack<TreeNode>(); /// <summary>
/// 先序遍历二叉树
/// </summary>
/// <param name="node"></param>
private void postNode(TreeNode node)
{
if (node != null)
{
S.Push(node);
if (node.left != null)
{
postNode(node.left);
}
if (node.right != null)
{
postNode(node.right);
}
}
} /// <summary>
/// 以node为根节点的树的val之和
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private int SumNode(TreeNode node)
{
int sum = ;
if (node != null)
{
sum += node.val;
if (node.left != null)
{
sum += SumNode(node.left);
} if (node.right != null)
{
sum += SumNode(node.right);
}
}
return sum;
} /// <summary>
/// 获取每个节点的tilt值
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private int GetTilt(TreeNode node)
{
if (node == null)
{
return ;
}
else if (node.left == null && node.right == null)
{
return ;
}
else
{
//计算左子树sum需要遍历左子树,然后累加每个节点的值
var left = ;
if (node.left != null)
{
left = SumNode(node.left);
}
//计算右子树sum需要遍历右子树,然后累加每个节点的值
var right = ;
if (node.right != null)
{
right = SumNode(node.right);
}
//根节点的tilt等于左右子树sum之差,//因此需要计算左子树sum和右子树sum
var tilt = Math.Abs(left - right);
return tilt;
}
} public int FindTilt(TreeNode root)
{
if (root != null)
{
//先遍历每个节点
postNode(root); //获得节点列表
var list = S.ToList(); //计算每个节点的tilt,再加到一起
var sum = ;
foreach (var l in list)
{
sum += GetTilt(l);
}
return sum;
}
else
{
return ;
}
}
}
https://leetcode.com/problems/binary-tree-tilt/#/description
leetcode563的更多相关文章
- [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 ...
 - Leetcode563.Binary Tree Tilt二叉树的坡度
		
给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点的的坡度是0. 整个树的坡度就是其所有节点的坡度之和. 示例: 输入: 1 / ...
 - LeetCode563. 二叉树的坡度
		
题目 1 class Solution { 2 public: 3 int ans = 0; 4 int findTilt(TreeNode* root) { 5 postOrder(root); 6 ...
 
随机推荐
- 【Demo】Tree.js实例
			
Three.js是通过对WebGL接口的封装与简化而形成的一个易用的图形库. 简单点的说法:WebGL可以看成是浏览器给我们提供的接口,在javascript中可以直接用这些API进行3D图形的绘制: ...
 - socket中 emit和on的写法
			
socket.emit('action');表示发送了一个action命令,命令是字符串的,在另一端接收时,可以这么写: socket.on('action',function(){...});soc ...
 - 201621123005《Java程序设计》第十三次实验总结
			
<Java程序设计>第十三周实验总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主 ...
 - EM算法及其应用(一)
			
EM算法及其应用(一) EM算法及其应用(二): K-means 与 高斯混合模型 EM算法是期望最大化 (Expectation Maximization) 算法的简称,用于含有隐变量的情况下,概率 ...
 - Scrum立会报告+燃尽图 06
			
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2289] 版本控制:https://git.coding.net/liuyy08 ...
 - Django——ContentType(与多个表建立外键关系)及ContentType-signals的使用
			
一.ContentType 在django中,有一个记录了项目中所有model元数据的表,就是ContentType,表中一条记录对应着一个存在的model,所以可以通过一个ContentType表的 ...
 - C# Seal用法
			
C# Seal用法 sealed的中文意思是密封,故名思义,就是由它修饰的类或方法将不能被继承或是重写. sealed关键字的作用: 在类声明中使用sealed可防止其它类继承此类:在方法声明 ...
 - erhai系统使用_web
			
使用前说明1.安装mysql数据库,安装数据库管理器EMS(SQL Manager Lite for MySQL),将数据库导入数据库管理器: 注意对配置文件my.ini的修改.2.启动resin W ...
 - 转:Android-apt
			
转自http://blog.csdn.net/zjbpku/article/details/22976291 What is this? The Android-apt plugin assists ...
 - Yii 初识
			
接管一个Yii的系统,因为没有文档,所以非常上火. 01 查版本 Yii::getVersion(); 02 生成webapp Yii 是支持通过命令行生成webapp的.其中, yiic.bat是W ...