二叉树单色路径最长&&穿珠子
对树的操作,特别理解递归的好处。
//对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的
//另一点结束而形成的路径,而路径的长度就是经过的点的数量(包括起点和终点)。而这里我们所说的单色路径自然就是只经过一种颜色的点的路径。
//你需要找到这棵树上最长的单色路径。
//给定一棵二叉树的根节点(树的点数小于等于300,请做到O(n)的复杂度),请返回最长单色路径的长度。
//这里的节点颜色由点上的权值表示,权值为1的是黑点,为0的是白点。
//这题用动态规划来求解。需要用到一对引用传值(white和black)来记录每个节点的左右孩子节点的黑白路径长度值传递上来。
//如:lhswhite ,lhsblack ,rhswhite,rhsblack分别表示两个孩子节点的黑白最长路径长度
//分两类情况:
//(1)当父节点为黑时:其white = 0,black = max(lhsblack + 1, rhsblack + 1),它的最长单色路径长度为lhsblack + rhsblack + 1;
//(2)当父节点为白时:其black = 0,white = max(lhswhite + 1, rhswhite + 1),它的最长单色路径长度为lhswhite + rhswhite + 1;
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(nullptr), right(nullptr) {
}
};
//也不知道为什么就是不AC,和下面的有区别吗????
class LongestPath {
public:
;
int findPath(TreeNode* root) {
// write code here
if (root==nullptr)
{
;
}
, black = ;
path(root,white,black);
return result;
}
void path(TreeNode* root, int &white, int &balck)
{
if (root->right == nullptr&&root->right == nullptr)
{
)
{
balck = ; white = ;
}
else
{
balck = ; white = ;
}
}
else
{
, lwhite = ;
, rwhite = ;
if (root->right != nullptr)
{
path(root->right, rwhite, rblack);
}
if (root->left != nullptr)
{
path(root->left, lwhite, lblack);
}
)
{
> result)
result = lblack + rblack + ;
white = ;
balck = (lblack + ) > (rblack + ) ? (lblack + ) : (rblack + );
}
else
{
> result)
result = rwhite + lwhite + ;
balck = ;
white = (lwhite + ) > (rwhite + ) ? (lwhite + ) : (rwhite + );
}
}
}
};
class LongestPath {
public:
;
int findPath(TreeNode* root) {
// write code here
, black = ;
dfsPath(root, white, black);
return ret;
}
void dfsPath(TreeNode* root, int &white, int &black){
if (root->left == nullptr && root->right == nullptr){
){
white = ; black = ;
}
else{
white = ; black = ;
}
}
else{
, lhsblack = ;
, rhsblack = ;
if (root->left != nullptr) dfsPath(root->left, lhswhite, lhsblack);
if (root->right != nullptr) dfsPath(root->right, rhswhite, rhsblack);
){
> ret) ret = lhsblack + rhsblack + ;
white = ;
black = lhsblack + > rhsblack + ? lhsblack + : rhsblack + ;
}
else{
> ret) ret = lhswhite + rhswhite + ;
black = ;
white = lhswhite + > rhswhite + ? lhswhite + : rhswhite + ;
}
}
}
};
参考:http://www.cnblogs.com/chenhuan001/p/5420368.html。找规律!
//现在A和B在玩一个游戏,这个游戏首先给了他们很多珠子,珠子有两种颜色,一种蓝色,一种黄色,我们假定两种珠子都有无限多。
//A需要选择n颗珠子(n为奇数),然后由B串成一串项链(顺序由B确定, 这里的项链也就是一个环)。假如在最后串成的项链中,
//A能够找到两个不同位置的蓝色珠子,并在这两处把这个项链断开成两段,其中一段恰好长度为(n + 1) / 2那么A就胜利了,
//注意这里为整数截断除法且这个长度是不包括选出的两颗珠子的。现在请你计算出A至少要选择多少颗蓝色珠子,才能保证无论B怎么串,
//他都能获胜。举个例子,当A选了7颗珠子,其中有3颗蓝珠子,那么如果B串的项链为"蓝蓝红红红红蓝",则A能获胜,
//若B串的项链为"蓝蓝红红蓝红红",则A不能获胜。
//
//输入描述 :
//给定一个整数n,为A要选出的珠子颗数.
//
//
//输出描述 :
// 请返回A至少要选的蓝珠子颗数。
//
// 输入例子 :
//7
//
//输出例子 :
class Chain {
public:
int findK(int n) {
// write code here
== ) ;
) / ;
}
};
二叉树单色路径最长&&穿珠子的更多相关文章
- 二叉树单色路径最长&&穿珠子
对树的操作,特别理解递归的好处. //对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的 //另一点结束而形成的路径,而路 ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LintCode-376.二叉树的路径和
二叉树的路径和 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 样例 给定一个二叉树,和 目标值 = 5: 返回: [ ...
- Java实现求二叉树的路径和
题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [LeetCode] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- lintcode:二叉树的路径和
题目 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 解题 下面有个小bug 最后比较的时候是叶子节点为空,左右都有叶子结点,所 ...
随机推荐
- c# 串行【序列化】和解串【反序列化】
C# 串行[序列化]和解串[反序列化] 一. 什么是序列化和反序列话呢? 相信我们做程序的都会遇到这种情况,需要将C#中某一个结构很复杂的类的对象存储起来,或者通过网路传输到远程的客户端程序中去, ...
- HttpClient Post Form提交文件/二进制数据
HttpClient httpClient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(url); Multipar ...
- 堆的 两种实现 (数组和STL)
基本思想: 两种操作都跟树的深度成正比,所以复杂度 O(log(n)) ; push():在向堆中插入数值时,首先在堆的末尾插入该数值,然后不断向上提直到没有大小颠倒为止. pop(): 从堆中取出 ...
- mongodb unset/set 删除/增加字段
删除全部文档的name字段 db.users.update({},{$unset: {"name":""}},{nulti:true}) 增加全部文档的name ...
- 谈谈 char *num="123";和char num[4]="123";的区别
最近写程序的时候发现这样一个问题 #include<iostream> #include <string.h> using namespace std; void revers ...
- JS生成指定长度的随机数
/** * 生成指定长度的UUID * @param len * @param radix * @returns uuid * eg: createUUID(8, 2) "01001010& ...
- 【第一篇】说说MVC+EF easyui dataGrid 动态加载分页表格
首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...
- jstl表达式
JSTL标签库 1.什么是JSTL JSTL是apache对EL表达式的拓展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便, 它与JSP动作标签一样,只不过它不是JSP ...
- mokoid android open source HAL hacking in a picture
/************************************************************************** * mokoid android HAL hac ...
- (一) 从零开始搭建Spark Standalone集群环境搭建
本文主要讲解spark 环境的搭建 主机配置 4核8线程,主频3.4G,16G内存 虚拟环境: VMWare 虚拟环境系统:Ubuntu 14.10 虚拟机运行环境: jdk-1.7.0_79(64 ...