有向二叉树中严格递增和递减路径的计数
给定一个有N个节点的有向二叉树,其边从父节点指向子节点,任务是计算严格递增和递减路径的数量。
注意:路径从根开始,到任何叶子结束。
例子:
Input: N = 6
tree = 6
/ \
4 7
\ / \
5 6 8
Output: 10 8
Explanation: For the above binary tree, the strictly increasing paths are :
All the paths consisting of a single node are = 6.
The paths containing two nodes are: 4->5, 7->8, 6->7 = 3.
The paths containing three nodes are: 6->7->8 = 1.
Hence, the count of strictly increasing paths is 6 + 3 + 1 = 10.
For the above binary tree, the strictly decreasing paths are:
All the paths consisting of a single node are = 6.
The paths containing two nodes are: 6->4, 7->6 = 2.
Hence, the count of strictly decreasing paths is 6 + 2 = 8.
Input: N = 5
tree 15
/
8
\
2
/ \
13 9
Output: 7 8
Explanation:
For the above binary tree, the strictly increasing paths are:
All the paths consisting of a single node are = 5.
The paths containing two nodes are: 2->9, 2->13 = 2.
Hence, the count of strictly increasing paths is 5 + 2 = 7.
For the above binary tree, the strictly decreasing paths are:
All the paths consisting of a single node are = 5.
The paths containing two nodes are:15->8, 8->2 = 2.
The paths containing three nodes are: 15->8->2 = 1.
Hence, the count of strictly decreasing paths is 5 + 2 + 1 = 8.
方法:可以根据以下观察解决问题:
If a path starting from any node u is strictly increasing and its value is greater than the value of its parent (say v), then all the striclty increasing paths starting from u are also strictly increasing paths when the starting node is v. The same is true in case of strictly decreasing paths.
从以上观察,可以借助递归来解决问题。请按照以下步骤解决问题:
- 使用递归函数从根开始遍历树,并为每个节点返回从该节点开始的严格递增和严格递减路径的计数。
- 在每次递归中:
- 检查从当前节点到子节点(左或右)的路径是增加还是减少。
- 调用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)