二叉树中节点在 [L, R] 范围内的节点数
给定一个由N个节点和两个正整数 L 和 R 组成的二叉树,任务是找到其值在[L, R]范围内的节点的计数。
例子:
Input: Tree in the image below, L = 4, R = 15
Output: 2
Explanation: The nodes in the given Tree that lies in the range [4, 15] are {5, 10}.
Input: Tree in the image below, L = 8, R = 20
Output: 4
方法:给定的问题可以通过执行任何树遍历并保持其值在[L, R]范围内的节点的计数来解决。本文使用 DFS 遍历。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Class for node of the Tree
class Node {
public:
int val;
Node *left, *right;
};
// Function to create a new Binary node
Node* newNode(int item)
{
Node* temp = new Node();
temp->val = item;
temp->left = temp->right = NULL;
// Return the newly created node
return temp;
}
// Function to find the count of
// nodes in the given tree with
// their value in the range [1, N]
int countRange(Node* root, int low,
int high, int count)
{
int val = 0;
// If root exists
if (root != NULL) {
val += root->val >= low
&& root->val <= high
? 1
: 0;
}
// Otherwise return
else {
return 0;
}
// Add count of current node,
// count in left subtree, and
// count in the right subtree
count = val
+ countRange(root->left,
low, high, count)
+ countRange(root->right,
low, high, count);
// Return Answer
return count;
}
// Driver Code
int main()
{
Node* root = NULL;
root = newNode(20);
root->left = newNode(2);
root->right = newNode(10);
root->right->left = newNode(2);
root->right->right = newNode(5);
int L = 4, R = 15;
cout << countRange(root, L, R, 0);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Class for node of the Tree
static class Node {
int val;
Node left, right;
};
// Function to create a new Binary node
static Node newNode(int item)
{
Node temp = new Node();
temp.val = item;
temp.left = temp.right = null;
// Return the newly created node
return temp;
}
// Function to find the count of
// nodes in the given tree with
// their value in the range [1, N]
static int countRange(Node root, int low,
int high, int count)
{
int val = 0;
// If root exists
if (root != null) {
val += root.val >= low
&& root.val <= high
? 1
: 0;
}
// Otherwise return
else {
return 0;
}
// Add count of current node,
// count in left subtree, and
// count in the right subtree
count = val
+ countRange(root.left,
low, high, count)
+ countRange(root.right,
low, high, count);
// Return Answer
return count;
}
// Driver Code
public static void main(String[] args)
{
Node root = null;
root = newNode(20);
root.left = newNode(2);
root.right = newNode(10);
root.right.left = newNode(2);
root.right.right = newNode(5);
int L = 4, R = 15;
System.out.print(countRange(root, L, R, 0));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program for the above approach
# Class for Node of the Tree
class Node:
def __init__(self, val):
self.val = val;
self.left = None;
self.right = None;
# Function to create a new Binary Node
def newNode(item):
temp = Node(item);
# Return the newly created Node
return temp;
# Function to find the count of
# Nodes in the given tree with
# their value in the range [1, N]
def countRange(root, low, high, count):
val = 0;
# If root exists
if (root != None):
val += 1 if(root.val >= low and root.val <= high) else 0;
# Otherwise return
else:
return 0;
# Add count of current Node,
# count in left subtree, and
# count in the right subtree
count = val + countRange(root.left, low, high, count) + countRange(root.right, low, high, count);
# Return Answer
return count;
# Driver Code
if __name__ == '__main__':
root = None;
root = newNode(20);
root.left = newNode(2);
root.right = newNode(10);
root.right.left = newNode(2);
root.right.right = newNode(5);
L = 4;
R = 15;
print(countRange(root, L, R, 0));
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG{
// Class for node of the Tree
class Node {
public int val;
public Node left, right;
};
// Function to create a new Binary node
static Node newNode(int item)
{
Node temp = new Node();
temp.val = item;
temp.left = temp.right = null;
// Return the newly created node
return temp;
}
// Function to find the count of
// nodes in the given tree with
// their value in the range [1, N]
static int countRange(Node root, int low,
int high, int count)
{
int val = 0;
// If root exists
if (root != null) {
val += root.val >= low
&& root.val <= high
? 1
: 0;
}
// Otherwise return
else {
return 0;
}
// Add count of current node,
// count in left subtree, and
// count in the right subtree
count = val
+ countRange(root.left,
low, high, count)
+ countRange(root.right,
low, high, count);
// Return Answer
return count;
}
// Driver Code
public static void Main(String[] args)
{
Node root = null;
root = newNode(20);
root.left = newNode(2);
root.right = newNode(10);
root.right.left = newNode(2);
root.right.right = newNode(5);
int L = 4, R = 15;
Console.Write(countRange(root, L, R, 0));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(1)