找出最大可能三角弦的和
find sum of maximum possible triangular chord
我有一个二叉树
2
/
3 4
/
5 1 8
/ /
1 6 9 2
4
我想找到给定树中节点的maximum possible triangular chord info sum
(在任意两个叶子和同时具有左子和右子的节点之间)
三角形和弦将是
三角弦:
想象任意两片叶子之间的一条线,向上找到根,找到一个共同的父结点(可以是父结点、祖父母结点、祖父母结点,甚至根结点本身)。当向上移动时,对于每个叶子(对于任何叶子)我们只能向左向左向左....所以要么只有对,对,对,对…等)意味着(左叶只会向上移动right
,右叶只会向上移动left
.....所以对于任何一片叶子,we can not move in both direction while moving upwards
)。现在我们得到一个三角形。其中a side may contain any no. of nodes/links possible
..现在,如果那是triangular shape does not contain any extra internal branches
。这个三角形就是三角弦。
请记住every leaf node is also always a triangular chord
(这只是创建默认情况下,如果二叉树没有任何三角形和弦)
maximum triangular chord will be that triangular chord
which have maximum total in sum of all its node info.
我们被要求return that maximum total.
If we do not have triangular shaped chord..
then we have to return the leaf with maximum info.
例如 8
/
2 3
3
是三角弦
8
/
2 3
4 1
只有单节点4的子树是最大三角弦(因为它的和大于另一个单节点1的三角弦),而不是整个树都是三角弦
8
/
2 3
/
4 3
是三角弦
所以问题第一行第一个树的解是
8+9+2+4 = 23
我被这个问题难住了。
我有一个粗略的方法
我将递归调用leftchild作为子树的根,并找到左最大三角和弦和右子节点作为子树的根节点也是一样。
添加leftmax和rightmax中的Max,并添加到rood节点并返回
在c++ mycode是:
int maxtri(node* n)
{
if(n)
{
lsum = maxtri(n->left);
rsum = maxtri(n->right);
k = maxof(lsum,rsum);
return (n->info + k);
}
}
edit: my另一个递归方法
int l =0, r =0;
int maxtri(node* n)
{
if (n == NULL) return 0;
if (!(n->left) && !(n->right)) return n->info;
if ((n->left) && (n->right))
{
l = maxtri(n->left);
r = maxtri(n->right);
}
if ((n->left) && !(n->right))
{
l = l + maxtri(n->left);
}
if (!(n->left) && (n->right))
{
r = r + maxtri(n->right);
}
return (l+r+n->info);
}
我怀疑我的方法。
谁能给出另一个解决方案?? ?这个逻辑怎么样?
此外,对于部分计算节点应留有& &;右节点,或者应该是叶节点。
注意:我没有正确地测试它,但我相信它应该工作。
// Node by Node traverse the tree
void addSum(Node *head, vector<int>& sum)
{
if (head == NULL)
return;
else {
int s = traverseThisNode(head);
sum.push_back(s); // Add to vector
addSum(head->left, sum);
addSum(head->right, sum);
}
}
// For each node traverse left & right
int traverseThisNode(Node *head)
{
if (head && head->left && head->right) {
Node *temp = head; // To traverse right portion of this node
int sum = head->value;
while(head->left) { // Traverse right
head = head->left;
sum = sum + head->value;
if (head->right) { // Condition to check if there is any branching
sum = 0;
break;
}
}
while(temp->right && sum != 0) { // Traverse Right now
temp = temp->right;
sum = sum + temp->value;
if (temp->left) { // Condition to check if there is any branching
sum = 0;
break;
}
}
return sum;
} else if (head && !head->left && !head->right) {
return head->value; // To add leaf node
}
return 0;
}
Now you have vector containing all the value of triangular in the tree, traverse it and
find the maximum.
int maximum()
{
// Traverse the vector "sum" & find the maximum
}
我为我的方法编写伪代码,就我对这个问题的理解而言。
Max = min_value; //possibly 0 if no negative value is allowed for nodes.
sum = 0;
for each node in the tree
temp = node;
sum+= temp->data //collects data at the current level, the current level may be leaf too.
Until temp->left is not null, // Traversing uni-directionally to the left most deep and collecting data.
temp = temp->left
sum+=temp->data
Until temp->right is not null, // Traversing uni-directionally to the right most deep and collecting data.
temp = temp->right
sum+= temp->data
if(sum > Max)
Max = sum;
sum = 0;
print Max;
- 三角化元组
- 为什么这个Pascal三角实现给了我尾随的零
- CGAL-三角网格,返回面对面映射
- 如何访问CGAL三维三角测量中的面
- CGAL 3D 周期性德劳内三角测量与信息的问题
- Fixed_alpha_shape_3() 会破坏或修改原始三角测量吗?
- C 是否具有对两个STD :: sets,vectors等进行三角比较的标准方法
- 编译和运行 CGAL 三角测量演示
- 如何计算三角数生成的运行时复杂度
- 在1个功能中打印相反的三角递归C
- 三角位置的颜色
- CGAL没有计算完整的德劳内三角测量
- 如何将三角孔贴片转换为曲面网格
- 优化德劳奈三角测量算法
- 是否有在 c++ 中使用三角分布生成随机数的函数
- 优化三角矩阵计算的 CUDA 内核执行
- 在OPENCV 3中找不到OpenCV 2 Delaunay三角剖分功能
- 特征三角视图的问题
- 多面体的三角剖分失败
- 特征:找到一个三角矩阵