Algorithm 二叉树中最多一圈的最长路径

Algorithm 二叉树中最多一圈的最长路径,algorithm,tree,binary-tree,Algorithm,Tree,Binary Tree,我在一次采访中遇到了这个问题 给定一棵二叉树,找出最多一圈的最长路径长度。 这条路的一端必须是一片叶子。另一端可以是叶或任何节点 转弯定义为: In tree1-> start from 1 and there is a turn at root 2 towards right, In tree2-> starts from 3 goes in left and there is a turn at 1 towards right , In tree3-> starts fro

我在一次采访中遇到了这个问题

给定一棵二叉树,找出最多一圈的最长路径长度。 这条路的一端必须是一片叶子。另一端可以是叶或任何节点

转弯定义为:

In tree1-> start from 1 and there is a turn at root 2 towards right,
In tree2-> starts from 3 goes in left and there is a turn at 1 towards right ,
In tree3-> starts from 1 goes in right and there is a turn at 3 towards left,

     2                 3                 1
    / \               /                   \
   1   3             1                     3
                      \                    /
                       2                  2
有人能帮忙吗?谢谢

编辑: 在采访中,我被问到这个问题,作为对树木直径问题的后续问题

我对树的直径的实现是这样的

变量“res”包含最终答案

int maxPathSumUtil(struct Node *root, int &res)
{
    // Base case
    if (root==NULL) return 0;

    // Find maximum sum in left and right subtree. Also find
    // maximum root to leaf sums in left and right subtrees
    // and store them in lLPSum and rLPSum
    int lLPSum = maxPathSumUtil(root->left, res);
    int rLPSum = maxPathSumUtil(root->right, res);

    // Find the maximum path sum passing through root
    int curr_sum = max(lLPSum+rLPSum+root->data);

    // Update res (or result) if needed
    if (res < curr_sum)
        res = curr_sum;

    // Return the maximum root to leaf path sum
    return max(lLPSum, rLPSum)+root->data;
}
int-maxPathSumUtil(结构节点*根,int&res)
{
//基本情况
if(root==NULL)返回0;
//在左子树和右子树中查找最大和。同时查找
//左子树和右子树中的最大根叶和
//并将其存储在lLPSum和rLPSum中
int lLPSum=maxPathSumUtil(root->left,res);
int rLPSum=maxPathSumUtil(根->右,res);
//查找通过根的最大路径和
int curr_sum=max(lLPSum+rLPSum+root->data);
//如果需要,更新res(或结果)
if(res数据;
}
最初,我认为我可以使用一个变量(如“turns”)来提出解决方案,并在每个节点跟踪turns变量


但是我在追踪二叉树中的转折点时有点迷茫。

我们可以使用动态规划

让我们:

我们有:

d[i] = max(d_up[i, R] + d_down[i, R],
           d_up[i, R] + d_down[i, L],
           d_up[i, L] + d_down[i, R],
           d_up[i, L] + d_down[i, L],
           d_down[i, L] + d_down[i, R])
这些都可以通过从任何节点进行单个DFS遍历来计算。伪代码:

DFS(i, direction):

  if i.left != null:
    d_up[i.left, L] = d_up[i, L] + 1
    d_down[i, L] = 1 + DFS(i.left, L)

  if i.right != null:
    d_up[i.right, R] = d_up[i, R] + 1
    d_down[i, R] = 1 + DFS(i.right, R)

  d[i] = max(d_up[i, R] + d_down[i, R],
             d_up[i, R] + d_down[i, L],
             d_up[i, L] + d_down[i, R],
             d_up[i, L] + d_down[i, L],
             d_down[i, L] + d_down[i, R])

  return 0

可能存在一些1比1的错误,如果是,请指出它们,但它应该可以工作。从技术上讲,复杂性是
O(n)

,但这不是备忘录吗?
DFS(i, direction):

  if i.left != null:
    d_up[i.left, L] = d_up[i, L] + 1
    d_down[i, L] = 1 + DFS(i.left, L)

  if i.right != null:
    d_up[i.right, R] = d_up[i, R] + 1
    d_down[i, R] = 1 + DFS(i.right, R)

  d[i] = max(d_up[i, R] + d_down[i, R],
             d_up[i, R] + d_down[i, L],
             d_up[i, L] + d_down[i, R],
             d_up[i, L] + d_down[i, L],
             d_down[i, L] + d_down[i, R])

  return 0