Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to the target and return the sum of all descendant (of target) node’s data which are vertically below the target node. Initially, you are at the root node.
Note: If the target node is not present in bst then return -1.
Examples:
Input:

Target = 35 Output: 32 Explanation: Vertically below 35 is 32.
Input:

Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52.
Approach: To solve the problem follow the below idea:
Idea is to use the property of the binary search tree. Check if the target value is present or not. If it is present return the sum of all descendant node’s data which are vertically below the target node else return -1.
Step-by-step approach:
- Implement a function to traverse down the BST and calculate the sum based on the given direction:
- If the direction is 0, add the node’s data to the result.
- Recursively traverse the left and right child nodes with updated directions (left: direction – 1, right: direction + 1).
- Implement a function to find the target node and calculate the sum vertically down from it:
- Start from the root and traverse the tree to find the target node by comparing data values.
- When the target node is found, set it as the target_node and calculate the sum vertically down from that node.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node( int val)
: data(val)
, left(nullptr)
, right(nullptr)
{
}
};
long long int res;
Node* target_node;
void down(Node* node, int direction)
{
if (node) {
if (direction == 0)
res += node->data;
down(node->left, direction - 1);
down(node->right, direction + 1);
}
}
void find(Node* node, int target)
{
if (target_node == NULL) {
if (node) {
if (node->data > target)
find(node->left, target);
else if (node->data < target)
find(node->right, target);
else {
target_node = node;
down(node, 0);
}
}
}
}
long long int verticallyDownBST(Node* root, int target)
{
res = -target;
target_node = NULL;
find(root, target);
if (res == -target)
return -1;
return res;
}
int main()
{
Node* root = new Node(25);
root->left = new Node(20);
root->right = new Node(35);
root->left->left = new Node(15);
root->left->right = new Node(22);
root->right->left = new Node(30);
root->right->right = new Node(45);
root->right->right = new Node(45);
root->right->left->right = new Node(32);
int target = 35;
long long int result = verticallyDownBST(root, target);
if (result != -1)
cout << "Largest Sum Vertically Down from Target ("
<< target << "): " << result << endl;
else
cout << "No valid target node found." << endl;
return 0;
}
|
Java
public class VerticalSumBST {
static class Node {
int data;
Node left;
Node right;
Node( int val)
{
data = val;
left = null ;
right = null ;
}
}
static long res;
static Node targetNode;
static void down(Node node, int direction)
{
if (node != null ) {
if (direction == 0 )
res += node.data;
down(node.left, direction - 1 );
down(node.right, direction + 1 );
}
}
static void find(Node node, int target)
{
if (targetNode == null ) {
if (node != null ) {
if (node.data > target)
find(node.left, target);
else if (node.data < target)
find(node.right, target);
else {
targetNode = node;
down(node, 0 );
}
}
}
}
static long verticallyDownBST(Node root, int target)
{
res = -target;
targetNode = null ;
find(root, target);
if (res == -target)
return - 1 ;
return res;
}
public static void main(String[] args)
{
Node root = new Node( 25 );
root.left = new Node( 20 );
root.right = new Node( 35 );
root.left.left = new Node( 15 );
root.left.right = new Node( 22 );
root.right.left = new Node( 30 );
root.right.right = new Node( 45 );
root.right.right = new Node( 45 );
root.right.left.right = new Node( 32 );
int target = 35 ;
long result = verticallyDownBST(root, target);
if (result != - 1 )
System.out.println(
"Largest Sum Vertically Down from Target ("
+ target + "): " + result);
else
System.out.println(
"No valid target node found." );
}
}
|
Python3
class Node:
def __init__( self , val):
self .data = val
self .left = None
self .right = None
res = 0
target_node = None
def down(node, direction):
global res
if node:
if direction = = 0 :
res + = node.data
down(node.left, direction - 1 )
down(node.right, direction + 1 )
def find(node, target):
global target_node
if target_node is None :
if node:
if node.data > target:
find(node.left, target)
elif node.data < target:
find(node.right, target)
else :
target_node = node
down(node, 0 )
def vertically_down_bst(root, target):
global res, target_node
res = - target
target_node = None
find(root, target)
if res = = - target:
return - 1
return res
if __name__ = = "__main__" :
root = Node( 25 )
root.left = Node( 20 )
root.right = Node( 35 )
root.left.left = Node( 15 )
root.left.right = Node( 22 )
root.right.left = Node( 30 )
root.right.right = Node( 45 )
root.right.left.right = Node( 32 )
target = 35
result = vertically_down_bst(root, target)
if result ! = - 1 :
print (f "Largest Sum Vertically Down from Target ({target}): {result}" )
else :
print ( "No valid target node found." )
|
C#
using System;
public class VerticalSumBST
{
public class Node
{
public int data;
public Node left;
public Node right;
public Node( int val)
{
data = val;
left = null ;
right = null ;
}
}
private static long res;
private static Node targetNode;
private static void Down(Node node, int direction)
{
if (node != null )
{
if (direction == 0)
res += node.data;
Down(node.left, direction - 1);
Down(node.right, direction + 1);
}
}
private static void Find(Node node, int target)
{
if (targetNode == null )
{
if (node != null )
{
if (node.data > target)
Find(node.left, target);
else if (node.data < target)
Find(node.right, target);
else
{
targetNode = node;
Down(node, 0);
}
}
}
}
private static long VerticallyDownBST(Node root, int target)
{
res = -target;
targetNode = null ;
Find(root, target);
if (res == -target)
return -1;
return res;
}
public static void Main()
{
Node root = new Node(25);
root.left = new Node(20);
root.right = new Node(35);
root.left.left = new Node(15);
root.left.right = new Node(22);
root.right.left = new Node(30);
root.right.right = new Node(45);
root.right.right = new Node(45);
root.right.left.right = new Node(32);
int target = 35;
long result = VerticallyDownBST(root, target);
if (result != -1)
Console.WriteLine( "Largest Sum Vertically Down from Target (" + target + "): " + result);
else
Console.WriteLine( "No valid target node found." );
}
}
|
Javascript
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
let res = 0;
let target_node = null ;
function down(node, direction) {
if (node) {
if (direction === 0) {
res += node.data;
}
down(node.left, direction - 1);
down(node.right, direction + 1);
}
}
function find(node, target) {
if (target_node === null ) {
if (node) {
if (node.data > target) {
find(node.left, target);
}
else if (node.data < target) {
find(node.right, target);
}
else {
target_node = node;
down(node, 0);
}
}
}
}
function verticallyDownBST(root, target) {
res = -target;
target_node = null ;
find(root, target);
if (res === -target) {
return -1;
}
return res;
}
let root = new Node(25);
root.left = new Node(20);
root.right = new Node(35);
root.left.left = new Node(15);
root.left.right = new Node(22);
root.right.left = new Node(30);
root.right.right = new Node(45);
root.right.left.right = new Node(32);
let target = 35;
let result = verticallyDownBST(root, target);
if (result !== -1) {
console.log(`Largest Sum Vertically Down from Target (${target}): ${result}`);
} else {
console.log( "No valid target node found." );
}
|
Output
Largest Sum Vertically Down from Target (35): 32
Time complexity: O(h), where ‘h’ is the height of the tree. In a balanced BST, it is O(log n), while in the worst case (skewed tree), it can be O(n). Auxiliary space: O(h),
|