📜  有向二叉树中严格递增和递减路径的计数

📅  最后修改于: 2022-05-13 01:56:07.999000             🧑  作者: Mango

有向二叉树中严格递增和递减路径的计数

给定一个有N个节点的有向二叉树,其边从父节点指向子节点,任务是计算严格递增和递减路径的数量。

注意:路径从根开始,到任何叶子结束。

例子:

方法:可以根据以下观察解决问题:

从以上观察,可以借助递归来解决问题。请按照以下步骤解决问题:

  • 使用递归函数从根开始遍历树,并为每个节点返回从该节点开始的严格递增和严格递减路径的计数。
  • 在每次递归中:
    • 检查从当前节点到子节点(左或右)的路径是增加还是减少。
    • 调用child的递归函数。
    • 如果从当前节点到子节点的路径分别增加或减少,则将增加或减少的路径计数添加到总计数中。
  • 返回为根获得的增加或减少路径的最终计数

以下是上述方法的实现:

C++
// C++ code to implement the approach
  
#include 
using namespace std;
  
// Following is the TreeNode
// class structure:
template 
class TreeNode {
public:
    T data;
    TreeNode* left;
    TreeNode* right;
  
    TreeNode(T data)
    {
        this->data = data;
        left = NULL;
        right = NULL;
    }
};
  
// Helper function to get the answer
pair countPathsHelper(TreeNode* root,
                                int& increase,
                                int& decrease)
{
    // If the root is NULL,
    // then no strictly increasing or
    // decreasing path.
    if (root == NULL) {
        return { 0, 0 };
    }
  
    // Call the function for both
    // the left and right child.
    pair p1
        = countPathsHelper(root->left,
                           increase, decrease);
    pair p2
        = countPathsHelper(root->right,
                           increase, decrease);
  
    // Initialize 'inc' and 'dec' to 1
    int inc = 1, dec = 1;
  
    // If the left child is not NULL.
    if (root->left != NULL) {
  
        // Check if the value is
        // increasing from parent to child.
        if (root->data < root->left->data) {
  
            // Add the count of strictly
            // increasing paths of
            // child to parent.
            inc += p1.first;
        }
  
        // Check if the value is decreasing
        // from parent to child.
        if (root->data > root->left->data) {
  
            // Add the count of strictly
            // decreasing paths of
            // child to parent.
            dec += p1.second;
        }
    }
    if (root->right != NULL) {
  
        // Check if the value is
        // increasing from parent to child.
        if (root->data < root->right->data) {
  
            // Add the count of strictly
            // increasing paths of
            // child to parent.
            inc += p2.first;
        }
  
        // Check if the value is
        // decreasing from parent to child
        if (root->data > root->right->data) {
  
            // Add the count of strictly
            // decreasing paths of
            // child to parent.
            dec += p2.second;
        }
    }
  
    // Add the total count of
    // strictly increasing paths to
    // the global strictly increasing
    // paths counter.
    increase += inc;
  
    // Add the total count of
    // strictly decreasing paths to
    // the global strictly
    // decreasing paths counter.
    decrease += dec;
    return { inc, dec };
}
  
// Function to count the paths
pair countPaths(TreeNode* root)
{
    // 'increase' stores the
    // total strictly increasing paths.
    int increase = 0;
  
    // 'decrease' stores the
    // total strictly decreasing paths.
    int decrease = 0;
  
    countPathsHelper(root, increase, decrease);
    return { increase, decrease };
}
  
// Driver code
int main()
{
    int N = 6;
    TreeNode* root
        = new TreeNode(N);
    root->left = new TreeNode(4);
    root->right = new TreeNode(7);
    root->left->right = new TreeNode(5);
    root->right->left = new TreeNode(6);
    root->right->right = new TreeNode(8);
  
    // Function call
    pair ans = countPaths(root);
    cout << ans.first << " " << ans.second;
    return 0;
}


输出
10 8

时间复杂度: O(N)
辅助空间: O(N)