653. Two Sum IV - Input is a BST

本文介绍了一种算法,该算法能在给定的二叉搜索树中寻找两个元素,这两个元素的和等于指定的目标值。通过使用队列进行层次遍历,并配合递归搜索方法来高效地完成任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a Binary Search Tree and a target number, return true if there
exist two elements in the BST such that their sum is equal to the
given target.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool searchKeyBST(struct TreeNode *root,int key) {
        if(root == NULL){
            return false;
        }    

        //printf("key = %d,root->val = %d \n\r",key,root->val);

        if(root->val == key){
            return true;
        }else if(key < root->val){
            return searchKeyBST(root->left,key);
        }else{
            return searchKeyBST(root->right,key);
        }
    }

    bool findTarget(TreeNode* root, int k) {
        int key = 0;
        queue<TreeNode *> qu;

        if(root == NULL){
            return false;
        }

        qu.push(root);        
        while(!qu.empty()){
            TreeNode * node = qu.front();
            key = k-(node->val);
            if(key != node->val){
                if(searchKeyBST(root,key)){       
                    return true;
                } 
            }
            qu.pop();
            if(node->left){
                qu.push(node->left);
            }
            if(node->right){
                qu.push(node->right);
            }
        }

        return false;
    }
};
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean searchKeyBST(TreeNode root,int key){
        if(root == null){
            return false;
        }

        if(key == root.val){
            return true;
        }else if(key < root.val){
            return searchKeyBST(root.left,key);
        }else{
            return searchKeyBST(root.right,key);
        }
    }

    public boolean findTarget(TreeNode root, int k) {
        if(root == null){
            return false;
        }

        Queue<TreeNode> qu = new LinkedList<TreeNode>();
        qu.add(root);

        while(qu.peek() != null){
            TreeNode node = qu.poll();
            int key = k - node.val;
            if(key != node.val){
                if(searchKeyBST(root,key)){
                    return true;
                }
            }

            if(root.left != null){
                qu.add(root.left);
            }
            if(root.right != null){
                qu.add(root.right);
            }
        }

        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值