Two Sum in BST - Pair with given sum

Last Updated : 28 May, 2026

Given the root of a Binary Search Tree (BST) of size n and an integer target, determine whether there exists a pair of distinct nodes in the BST such that the sum of their values is equal to the given target. Return true if such a pair exists; otherwise, return false.

Example:

Input: root = [7, 3, 8, 2, 4, N, 9], target = 12
Output: true
Explanation: In the given binary tree, there are two nodes (3 and 9) that add up to 12.

2056958160

Input: root = [9, 5, 10, 2, 6, N, 12], target = 23
Output: false
Explanation: In the given binary tree, there are no such two nodes that add up to 23.

bst-3
Try It Yourself
redirect icon

[Naive Approach] Check Complement for Every Node - O(n²) Time and O(h) Space

The idea is to traverse the BST and, for each node, search for its complement (target - node value) in the BST. If the complement exists in a different node, return true.

C++
#include <iostream>
using namespace std;

class Node {
  public:
    int data;
    Node* left;
    Node* right;
    Node(int d) {
        data = d;
        left = nullptr;
        right = nullptr;
    }
};

// Function to search for the Key in the BST
bool search(Node* root, int key, Node* temp) {
    
    // If the root is NULL, return false
    if (root == nullptr)
        return false;

    // Start search from the root
    Node* current = root;

    // Traverse the BST to find the target value `k`
    while (current != nullptr) {
        
        // If Key is found
        if (current->data == key && current != temp) 
            return true;
      
        // If key is smaller, move to the left
        else if (key < current->data) 
            current = current->left;
      
      	// If key is larger, move to the right
        else 
            current = current->right;
    }
  
	// Return false if no match is found
    return false; 
}

// Helper Function to find if there exists a pair 
// with a given sum in the BST
bool findTargetRec(Node* root, Node* current, int target) {
    if (current == nullptr)
        return false;
  	
  	// Check if the complement of the current node value exists
  	int complement = target - current->data;
  	if (search(root, complement, current)) 
      	return true;
	
    // Check for the pair in left and right subtree
    return findTargetRec(root, current->left, target) || 
      		findTargetRec(root, current->right, target);
}

// Function to find if there exists a pair 
// with a given sum in the BST
bool findTarget(Node* root, int target) {
  	return findTargetRec(root, root, target);
}

int main() {

    // Constructing the following BST:
    //         7
    //        / \
    //       3   8
    //      / \   \
    //     2   4   9

    Node *root = new Node(7);
    root->left = new Node(3);
    root->right = new Node(8);
    root->left->left = new Node(2);
    root->left->right = new Node(4);
    root->right->right = new Node(9);

    int target = 12;

    cout << (findTarget(root, target) ? "true" : "false");
    return 0;
}
Java
class Node {
    int data;
    Node left, right;

    Node(int d) {
        data = d;
        left = right = null;
    }
}

class GfG {
  
    // Function to search for the Key in the BST
    static boolean search(Node root, int key, Node temp) {
        
        // If the root is NULL, return false
        if (root == null)
            return false;

        // Start search from the root
        Node current = root;

        // Traverse the BST to find the target value `key`
        while (current != null) {
            
            // If Key is found
            if (current.data == key && current != temp)
                return true;

            // If key is smaller, move to the left
            else if (key < current.data)
                current = current.left;

            // If key is larger, move to the right
            else
                current = current.right;
        }

        // Return false if no match is found
        return false;
    }

    // Helper Function to find if there exists a pair 
    // with a given sum in the BST
    static boolean findTargetRec(Node root, Node current, int target) {
        if (current == null)
            return false;

        // Check if the complement of the current node value exists
        int complement = target - current.data;
        if (search(root, complement, current))
            return true;

        // Check for the pair in left and right subtree
        return findTargetRec(root, current.left, target) ||
               findTargetRec(root, current.right, target);
    }

    // Function to find if there exists a pair 
    // with a given sum in the BST
    static boolean findTarget(Node root, int target) {
        return findTargetRec(root, root, target);
    }

    public static void main(String[] args) {
        
        // Constructing the following BST:
        //         7
        //        / \
        //       3   8
        //      / \   \
        //     2   4   9

        Node root = new Node(7);
        root.left = new Node(3);
        root.right = new Node(8);
        root.left.left = new Node(2);
        root.left.right = new Node(4);
        root.right.right = new Node(9);

        int target = 12;

        System.out.println(findTarget(root, target) ? "true" : "false");
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

# Function to search for the Key in the BST
def search(root, key, temp):
    # If the root is NULL, return false
    if root is None:
        return False

    # Start search from the root
    current = root

    # Traverse the BST to find the target value `key`
    while current:
        # If Key is found
        if current.data == key and current != temp:
            return True

        # If key is smaller, move to the left
        elif key < current.data:
            current = current.left

        # If key is larger, move to the right
        else:
            current = current.right

    # Return false if no match is found
    return False

# Helper Function to find if there exists a pair 
# with a given sum in the BST
def findTargetRec(root, current, target):
    if current is None:
        return False

    # Check if the complement of the current node value exists
    complement = target - current.data
    if search(root, complement, current):
        return True

    # Check for the pair in left and right subtree
    return findTargetRec(root, current.left, target) or \
           findTargetRec(root, current.right, target)

# Function to find if there exists a pair 
# with a given sum in the BST
def findTarget(root, target):
    return findTargetRec(root, root, target)

if __name__ == "__main__":
    
    # Constructing the following BST:
    #         7
    #        / \
    #       3   8
    #      / \   \
    #     2   4   9

    root = Node(7)
    root.left = Node(3)
    root.right = Node(8)
    root.left.left = Node(2)
    root.left.right = Node(4)
    root.right.right = Node(9)

    target = 12

    print("true" if findTarget(root, target) else "false")
C#
using System;

class Node {
    public int data;
    public Node left, right;

    public Node(int d) {
        data = d;
        left = right = null;
    }
}

class GfG {
  
    // Function to search for the Key in the BST
    static bool Search(Node root, int key, Node temp) {
        
        // If the root is NULL, return false
        if (root == null)
            return false;

        // Start search from the root
        Node current = root;

        // Traverse the BST to find the target value `key`
        while (current != null) {
            
            // If Key is found
            if (current.data == key && current != temp)
                return true;

            // If key is smaller, move to the left
            else if (key < current.data)
                current = current.left;

            // If key is larger, move to the right
            else
                current = current.right;
        }

        // Return false if no match is found
        return false;
    }

    // Helper Function to find if there exists a pair 
    // with a given sum in the BST
    static bool FindTargetRec(Node root, Node current, int target) {
        if (current == null)
            return false;

        // Check if the complement of the current node value exists
        int complement = target - current.data;
        if (Search(root, complement, current))
            return true;

        // Check for the pair in left and right subtree
        return FindTargetRec(root, current.left, target) || 
               FindTargetRec(root, current.right, target);
    }

    // Function to find if there exists a pair 
    // with a given sum in the BST
    static bool FindTarget(Node root, int target) {
        return FindTargetRec(root, root, target);
    }

    static void Main(string[] args) {
        
        // Constructing the following BST:
        //         7
        //        / \
        //       3   8
        //      / \   \
        //     2   4   9

        Node root = new Node(7);
        root.left = new Node(3);
        root.right = new Node(8);
        root.left.left = new Node(2);
        root.left.right = new Node(4);
        root.right.right = new Node(9);

        int target = 12;

        Console.WriteLine(FindTarget(root, target) ? "true" : "false");
    }
}
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Function to search for the Key in the BST
function search(root, key, temp) {
    
    // If the root is NULL, return false
    if (root === null)
        return false;

    // Start search from the root
    let current = root;

    // Traverse the BST to find the target value `key`
    while (current !== null) {
        
        // If Key is found
        if (current.data === key && current !== temp)
            return true;

        // If key is smaller, move to the left
        else if (key < current.data)
            current = current.left;

        // If key is larger, move to the right
        else
            current = current.right;
    }

    // Return false if no match is found
    return false;
}

// Helper Function to find if there exists a pair 
// with a given sum in the BST
function findTargetRec(root, current, target) {
    if (current === null)
        return false;

    // Check if the complement of the current node value exists
    const complement = target - current.data;
    if (search(root, complement, current))
        return true;

    // Check for the pair in left and right subtree
    return findTargetRec(root, current.left, target) || 
           findTargetRec(root, current.right, target);
}

// Function to find if there exists a pair 
// with a given sum in the BST
function findTarget(root, target) {
    return findTargetRec(root, root, target);
}

// Driver Code

// Constructing the following BST:
//         7
//        / \
//       3   8
//      / \   \
//     2   4   9
const root = new Node(7);
root.left = new Node(3);
root.right = new Node(8);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.right.right = new Node(9);

let target = 12;

console.log(findTarget(root, target) ? "true" : "false");

Output
true

[Better Approach 1] Using Hash Set - O(n) time and O(n) space

The idea of this approach is to use a Hash Set to store the values of nodes visited during a traversal of the BST. While traversing each node, we check whether the complement value (target - current node value) already exists in the set. If it exists, it means a valid pair of distinct nodes has been found whose sum is equal to the target, and we return true. Otherwise, we insert the current node’s value into the set and continue the traversal.

C++
//Driver Code Starts
#include <iostream>
#include <unordered_set>
using namespace std;

class Node{
  public:
    int data;
    Node *left, *right;
    Node(int x){
        data = x;
        left = nullptr;
        right = nullptr;
    }
};

// Helper function to perform DFS and check
//Driver Code Ends

// for the required target.
bool dfs(Node *root, unordered_set<int> &st, int target){
  
    if (root == nullptr)
        return false;

    // Check if the complement (target - current node's value)
    // exists in the set
    if (st.find(target - root->data) != st.end())
        return true;

    // Insert the current node's value into the set
    st.insert(root->data);

    // Continue the search in the left and right subtrees
    return dfs(root->left, st, target) || dfs(root->right, st, target);
}

// Main function to check if two elements
// in the BST target to target
bool findTarget(Node *root, int target){
  
    // To store visited nodes' values
    unordered_set<int> st;

    // Start DFS from the root
    return dfs(root, st, target);
}

int main(){
    

//Driver Code Starts
    // Constructing the following BST:
    //         7
    //        / \
    //       3   8
    //      / \   \
    //     2   4   9

    Node *root = new Node(7);
    root->left = new Node(3);
    root->right = new Node(8);
    root->left->left = new Node(2);
    root->left->right = new Node(4);
    root->right->right = new Node(9);

    int target = 12;

    if (findTarget(root, target))
        cout << "true
";
    else
        cout << "false
";

    return 0;
}
//Driver Code Ends
Java
//Driver Code Starts
import java.util.HashSet;

class Node {
    int data;
    Node left, right;

    Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GfG {
  
    // Helper function to perform DFS and check
//Driver Code Ends

    // for the required target.
    static boolean dfs(Node root, HashSet<Integer> set, int target) {
        if (root == null)
            return false;

        // Check if the complement (target - current node's value)
        // exists in the set
        if (set.contains(target - root.data))
            return true;

        // Insert the current node's value into the set
        set.add(root.data);

        // Continue the search in the left and right subtrees
        return dfs(root.left, set, target) || dfs(root.right, set, target);
    }

    // Main function to check if two elements
    // in the BST target to target
    static boolean findTarget(Node root, int target) {
        HashSet<Integer> set = new HashSet<>();
        return dfs(root, set, target);
    }

    public static void main(String[] args) {
        

//Driver Code Starts
        // Constructing the following BST:
        //         7
        //        / \
        //       3   8
        //      / \   \
        //     2   4   9

        Node root = new Node(7);
        root.left = new Node(3);
        root.right = new Node(8);
        root.left.left = new Node(2);
        root.left.right = new Node(4);
        root.right.right = new Node(9);

        int target = 12;

        if (findTarget(root, target))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
//Driver Code Ends
Python
#Driver Code Starts
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None

# Helper function to perform DFS and check
# for the required target.
#Driver Code Ends

def dfs(root, st, target):
    if root is None:
        return False

    # Check if the complement (target - current node's value)
    # exists in the set
    if target - root.data in st:
        return True

    # Insert the current node's value into the set
    st.add(root.data)

    # Continue the search in the left and right subtrees
    return dfs(root.left, st, target) or dfs(root.right, st, target)

# Main function to check if two elements
# in the BST target to target
def findTarget(root, target):
    st = set()
    return dfs(root, st, target)

if __name__ == "__main__":
    
    # Constructing the following BST:

#Driver Code Starts
    #         7
    #        / \
    #       3   8
    #      / \   \
    #     2   4   9

    root = Node(7)
    root.left = Node(3)
    root.right = Node(8)
    root.left.left = Node(2)
    root.left.right = Node(4)
    root.right.right = Node(9)

    target = 12

    if findTarget(root, target):
        print("true")
    else:
        print("false")
#Driver Code Ends
C#
//Driver Code Starts
using System;
using System.Collections.Generic;

class Node {
    public int data;
    public Node left, right;

    public Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GfG {
  
    // Helper function to perform DFS and check
//Driver Code Ends

    // for the required target.
    static bool dfs(Node root, HashSet<int> set, int target) {
        if (root == null)
            return false;

        // Check if the complement (target - current node's value)
        // exists in the set
        if (set.Contains(target - root.data))
            return true;

        // Insert the current node's value into the set
        set.Add(root.data);

        // Continue the search in the left and right subtrees
        return dfs(root.left, set, target) || dfs(root.right, set, target);
    }

    // Main function to check if two elements
    // in the BST target to target
    static bool findTarget(Node root, int target) {
        HashSet<int> set = new HashSet<int>();
        return dfs(root, set, target);
    }

    static void Main(string[] args) {
        

//Driver Code Starts
        // Constructing the following BST:
        //         7
        //        / \
        //       3   8
        //      / \   \
        //     2   4   9

        Node root = new Node(7);
        root.left = new Node(3);
        root.right = new Node(8);
        root.left.left = new Node(2);
        root.left.right = new Node(4);
        root.right.right = new Node(9);

        int target = 12;

        if (findTarget(root, target))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
//Driver Code Ends
JavaScript
//Driver Code Starts
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Helper function to perform DFS and check
//Driver Code Ends

// for the required target.
function dfs(root, set, target) {
    if (root === null)
        return false;

    // Check if the complement (target - current node's value)
    // exists in the set
    if (set.has(target - root.data))
        return true;

    // Insert the current node's value into the set
    set.add(root.data);

    // Continue the search in the left and right subtrees
    return dfs(root.left, set, target) || dfs(root.right, set, target);
}

// Main function to check if two elements
// in the BST target to target
function findTarget(root, target) {
    const set = new Set();
    return dfs(root, set, target);
}

// Driver Code


//Driver Code Starts
// Constructing the following BST:
//         7
//        / \
//       3   8
//      / \   \
//     2   4   9
const root = new Node(7);
root.left = new Node(3);
root.right = new Node(8);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.right.right = new Node(9);

const target = 12;

if (findTarget(root, target)) {
    console.log("true");
} else {
    console.log("false");
}
//Driver Code Ends

Output
true

[Expected Approach 2] Using Inorder Traversal and Two Pointers - O(n) time and O(n) space

The idea of this approach is based on the property of a Binary Search Tree (BST), where an inorder traversal always produces elements in sorted order. We first perform an inorder traversal of the BST and store all node values in an auxiliary array. Since the BST inorder is sorted, the resulting array will also be sorted. Once we have the sorted array, we apply the two-pointer technique to find whether there exists a pair of elements whose sum is equal to the given target.

  • Perform inorder traversal of the BST
  • Store all node values in an array, which becomes sorted due to BST inorder property
  • Initialize two pointers: left = 0 and right = n - 1
  • While left < right, compute currentSum = arr[left] + arr[right]
  • If currentSum == target, return true
  • If currentSum < target, increment left pointer
  • If currentSum > target, decrement right pointer
  • If no valid pair is found after traversal, return false

Consider the BST: root = [7, 3, 8, 2, 4, N, 9], target = 12

250341388

Step 1: Inorder traversal of BST gives sorted array: [2, 3, 4, 7, 8, 9]

Step 2: Apply Two Pointers

  • left = 0 (2), right = 5 (9): sum = 11 -> less than 12 -> move left++
  • left = 1 (3), right = 5 (9): sum = 12 -> equal to target -> return true

Final Result: Pair found: (3, 9), hence, we return true.

C++
//Driver Code Starts
#include <iostream>
#include <vector>
using namespace std;

class Node {
  public:
    int data;
    Node* left;
    Node* right;

    Node(int d) {
        data = d;
        left = nullptr;
        right = nullptr;
    }
};

// Function to perform Inorder traversal and store the 
//Driver Code Ends

// elements in a vector
void inorderTraversal(Node* root, vector<int>& inorder) {
    if (root == nullptr)
        return;

    inorderTraversal(root->left, inorder);

    // Store the current node's value
    inorder.push_back(root->data);

    inorderTraversal(root->right, inorder);
}

bool findTarget(Node* root, int target) {
    
    // Create an auxiliary array and store Inorder traversal
    vector<int> inorder;
    inorderTraversal(root, inorder); 

    // Use two-pointer technique to find the pair with given sum
    int left = 0, right = inorder.size() - 1;

    while (left < right) {
        int currentSum = inorder[left] + inorder[right];

        // If the pair is found, return true
        if (currentSum == target) 
            return true;

        // If the current sum is less than the target, 
        // move the left pointer
        if (currentSum < target)
            left++;
      
          // If the current sum is greater than 
        // the target, move the right pointer
        else
            right--;
    }

    return false;
}

int main(){
    
    // Constructing the following BST:
    //         7

//Driver Code Starts
    //        / \
    //       3   8
    //      / \   \
    //     2   4   9

    Node *root = new Node(7);
    root->left = new Node(3);
    root->right = new Node(8);
    root->left->left = new Node(2);
    root->left->right = new Node(4);
    root->right->right = new Node(9);

    int target = 12;

    if (findTarget(root, target))
        cout << "true
";
    else
        cout << "false
";

    return 0;
}
//Driver Code Ends
Java
//Driver Code Starts
import java.util.ArrayList;

class Node {
    int data;
    Node left, right;

    Node(int data) {
        this.data = data;
        left = null;
        right = null;
    }
}

class GfG {
    
    // Function to perform Inorder traversal and store the 
    // elements in an array
//Driver Code Ends

    static void inorderTraversal(Node root, ArrayList<Integer> inorder) {
        if (root == null)
            return;

        inorderTraversal(root.left, inorder);

        // Store the current node's value
        inorder.add(root.data);

        inorderTraversal(root.right, inorder);
    }

    static boolean findTarget(Node root, int target) {
      
        // Create an auxiliary array and store Inorder traversal
        ArrayList<Integer> inorder = new ArrayList<>();
        inorderTraversal(root, inorder);

        // Use two-pointer technique to find the pair with given sum
        int left = 0, right = inorder.size() - 1;

        while (left < right) {
            int currentSum = inorder.get(left) + inorder.get(right);

            // If the pair is found, return true
            if (currentSum == target)
                return true;

            // If the current sum is less than the target, 
            // move the left pointer
            if (currentSum < target)
                left++;
          
            // If the current sum is greater than 
            // the target, move the right pointer
            else
                right--;
        }

        return false;
    }

    public static void main(String[] args) {
        
        // Constructing the following BST:
        //         7
        //        / \

//Driver Code Starts
        //       3   8
        //      / \   \
        //     2   4   9

        Node root = new Node(7);
        root.left = new Node(3);
        root.right = new Node(8);
        root.left.left = new Node(2);
        root.left.right = new Node(4);
        root.right.right = new Node(9);

        int target = 12;

        if (findTarget(root, target))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
//Driver Code Ends
Python
#Driver Code Starts
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

# Function to perform Inorder traversal and store the 
# elements in an array
#Driver Code Ends

def inorderTraversal(root, inorder):
    if root is None:
        return

    inorderTraversal(root.left, inorder)

    # Store the current node's value
    inorder.append(root.data)

    inorderTraversal(root.right, inorder)

def findTarget(root, target):
    
    # Create an auxiliary array and store Inorder traversal
    inorder = []
    inorderTraversal(root, inorder)

    # Use two-pointer technique to find the pair with given sum
    left, right = 0, len(inorder) - 1

    while left < right:
        currentSum = inorder[left] + inorder[right]

        # If the pair is found, return true
        if currentSum == target:
            return True

        # If the current sum is less than the target, 
        # move the left pointer
        if currentSum < target:
            left += 1
          
        # If the current sum is greater than 
        # the target, move the right pointer
        else:
            right -= 1

    return False

if __name__ == "__main__":
    
    # Constructing the following BST:
    #         7

#Driver Code Starts
    #        / \
    #       3   8
    #      / \   \
    #     2   4   9

    root = Node(7)
    root.left = Node(3)
    root.right = Node(8)
    root.left.left = Node(2)
    root.left.right = Node(4)
    root.right.right = Node(9)

    target = 12

    if findTarget(root, target):
        print("true")
    else:
        print("false")
#Driver Code Ends
C#
//Driver Code Starts
using System;
using System.Collections.Generic;

class Node {
    public int data;
    public Node left, right;

    public Node(int data) {
        this.data = data;
        left = right = null;
    }
}

class GfG {

    // Function to perform Inorder traversal and store the 
    // elements in a list
//Driver Code Ends

    static void inorderTraversal(Node root, List<int> inorder) {
        if (root == null)
            return;

        inorderTraversal(root.left, inorder);

        // Store the current node's value
        inorder.Add(root.data);

        inorderTraversal(root.right, inorder);
    }

    static bool findTarget(Node root, int target) {
      
        // Create an auxiliary list and store Inorder traversal
        List<int> inorder = new List<int>();
        inorderTraversal(root, inorder);

        // Use two-pointer technique to find the pair with given sum
        int left = 0, right = inorder.Count - 1;

        while (left < right) {
            int currentSum = inorder[left] + inorder[right];

            // If the pair is found, return true
            if (currentSum == target)
                return true;

            // If the current sum is less than the target, 
            // move the left pointer
            if (currentSum < target)
                left++;
          
            // If the current sum is greater than 
            // the target, move the right pointer
            else
                right--;
        }

        return false;
    }

    static void Main(string[] args) {
        
        // Constructing the following BST:
        //         7
        //        / \

//Driver Code Starts
        //       3   8
        //      / \   \
        //     2   4   9

        Node root = new Node(7);
        root.left = new Node(3);
        root.right = new Node(8);
        root.left.left = new Node(2);
        root.left.right = new Node(4);
        root.right.right = new Node(9);

        int target = 12;

        if (findTarget(root, target))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
//Driver Code Ends
JavaScript
//Driver Code Starts
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

// Function to perform Inorder traversal and store the 
// elements in an array
//Driver Code Ends

function inorderTraversal(root, inorder) {
    if (root === null)
        return;

    inorderTraversal(root.left, inorder);

    // Store the current node's value
    inorder.push(root.data);

    inorderTraversal(root.right, inorder);
}

function findTarget(root, target) {

    // Create an auxiliary array and store Inorder traversal
    let inorder = [];
    inorderTraversal(root, inorder);

    // Use two-pointer technique to find the pair with given sum
    let left = 0, right = inorder.length - 1;

    while (left < right) {
        let currentSum = inorder[left] + inorder[right];

        // If the pair is found, return true
        if (currentSum === target)
            return true;

        // If the current sum is less than the target, 
        // move the left pointer
        if (currentSum < target)
            left++;
          
        // If the current sum is greater than 
        // the target, move the right pointer
        else
            right--;
    }

    return false;
}

// Driver Code

// Constructing the following BST:
//         7
//        / \

//Driver Code Starts
//       3   8
//      / \   \
//     2   4   9
const root = new Node(7);
root.left = new Node(3);
root.right = new Node(8);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.right.right = new Node(9);

const target = 12;

if (findTarget(root, target)) {
    console.log("true");
} else {
    console.log("false");
}
//Driver Code Ends

Output
true
Comment