二叉树单色路径最长&&穿珠子
对树的操作,特别理解递归的好处。
//对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的
//另一点结束而形成的路径,而路径的长度就是经过的点的数量(包括起点和终点)。而这里我们所说的单色路径自然就是只经过一种颜色的点的路径。
//你需要找到这棵树上最长的单色路径。
//给定一棵二叉树的根节点(树的点数小于等于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 result = ; int findPath(TreeNode* root) {
// write code here
if (root==nullptr)
{
return ;
}
int white = , black = ;
path(root,white,black);
return result;
}
void path(TreeNode* root, int &white, int &balck)
{
if (root->right == nullptr&&root->right == nullptr)
{
if (root->val == )
{
balck = ; white = ;
}
else
{
balck = ; white = ;
}
}
else
{
int lblack = , lwhite = ;
int rblack = , rwhite = ;
if (root->right != nullptr)
{
path(root->right, rwhite, rblack);
}
if (root->left != nullptr)
{
path(root->left, lwhite, lblack);
}
if (root->val == )
{
if (lblack + rblack + > result)
result = lblack + rblack + ;
white = ;
balck = (lblack + ) > (rblack + ) ? (lblack + ) : (rblack + );
}
else
{
if (rwhite + lwhite + > result)
result = rwhite + lwhite + ;
balck = ;
white = (lwhite + ) > (rwhite + ) ? (lwhite + ) : (rwhite + );
}
}
}
}; class LongestPath {
public:
int ret = ;
int findPath(TreeNode* root) {
// write code here
int white = , black = ;
dfsPath(root, white, black);
return ret;
}
void dfsPath(TreeNode* root, int &white, int &black){
if (root->left == nullptr && root->right == nullptr){
if (root->val == ){
white = ; black = ;
}
else{
white = ; black = ;
}
}
else{
int lhswhite = , lhsblack = ;
int rhswhite = , rhsblack = ;
if (root->left != nullptr) dfsPath(root->left, lhswhite, lhsblack);
if (root->right != nullptr) dfsPath(root->right, rhswhite, rhsblack);
if (root->val == ){
if (lhsblack + rhsblack + > ret) ret = lhsblack + rhsblack + ;
white = ;
black = lhsblack + > rhsblack + ? lhsblack + : rhsblack + ;
}
else{
if (lhswhite + rhswhite + > 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
if (n % == ) return n / ;
else return (n + ) / ;
}
};
二叉树单色路径最长&&穿珠子的更多相关文章
- 二叉树单色路径最长&&穿珠子
对树的操作,特别理解递归的好处. //对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的 //另一点结束而形成的路径,而路 ...
- 二叉树系列 - 二叉树里的最长路径 例 [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 最后比较的时候是叶子节点为空,左右都有叶子结点,所 ...
随机推荐
- USACO Section 4.2: Drainage Ditches
最大流的模板题 /* ID: yingzho1 LANG: C++ TASK: ditch */ #include <iostream> #include <fstream> ...
- asp.net mvc4使用百度ueditor编辑器
原文 http://www.cnblogs.com/flykai/p/3285307.html 已测试 相当不错 前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好 ...
- jsp中@import导入外部样式表与link链入外部样式表的区别
昨天碰到同事问了一个问题,@impor导入外部样式与link链入外部样式的优先级是怎样的,为什么实验的结果是按照样式表导入后的位置来决定优先级.今天就这个问题具体总结如下: 先解释一下网页添加cs ...
- [CCF2015.12]题解
201512-1 数位之和 水题一个,取模除以10胡搞即可(不知道字符串为什么不行 #include <algorithm> #include <iostream> #incl ...
- phpStorm连接mysql
小结:牛逼的IDE
- XML文件读取工具类
/// <summary> /// Author: jiangxiaoqiang /// </summary> public class XmlReader { //===== ...
- C#分页类
using System.Linq; using System.Collections.Generic; namespace CommonLibrary { public class PagedLis ...
- Machine Learning for hackers读书笔记(九)MDS:可视化地研究参议员相似性
library('foreign') library('ggplot2') data.dir <- file.path('G:\\dataguru\\ML_for_Hackers\\ML_for ...
- CodeForces ZeptoLab Code Rush 2015
拖了好久的题解,想想还是补一下吧. A. King of Thieves 直接枚举起点和5个点之间的间距,进行判断即可. #include <bits/stdc++.h> using na ...
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated
As indicated by the warning, to turn off the nonstandard behaviors, enable the new explicit_defaults ...