LintCode 689. Two Sum IV - Input is a BST

本文探讨了在二叉搜索树中寻找两个数使其和等于给定值的问题,提供了两种解决方案:一是结合中序遍历与二叉搜索树特性进行查找,二是利用层次遍历配合hashmap实现。第一种方法避免使用额外空间,第二种方法则通过hashmap提高查找效率。
  1. Two Sum IV - Input is a BST
    中文English
    Given a binary search tree and a number n, find two numbers in the tree that sums up to n.

Example
Example1

Input:
{4,2,5,1,3}
3
Output: [1,2] (or [2,1])
Explanation:
binary search tree:
4
/
2 5
/
1 3
Example2

Input:
{4,2,5,1,3}
5
Output: [2,3] (or [3,2])
Notice
Without any extra space.

注意:这题要求不用extra space,所以不能保存inOrderTraversal的结果,也不能用set或map之类。

解法1:
一边inOrderTraversal一边BST查找,效率好像不是很高 (O(nlogn))?
代码如下:

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /*
     * @param : the root of tree
     * @param : the target sum
     * @return: two number from tree witch sum is n
     */
    vector<int> twoSum(TreeNode * root, int n) {
        if (!root || n <= 0) return vector<int>();
        origRoot = root;
        inOrderTraversal(root, n);
        return result;
    }
    
private:
    TreeNode * origRoot;
    vector<int> result;
    
    bool bst(TreeNode * root, int target) {
        if (!root) return false;
        if (root->val > target) return bst(root->left, target);
        if (root->val < target) return bst(root->right, target);
        return true;
    }
    
    void inOrderTraversal(TreeNode * root, int n) {
        if (!root) return;
        inOrderTraversal(root->left, n);
        if (bst(origRoot, n - root->val)) result = vector<int>{root->val, n - root->val};
        inOrderTraversal(root->right, n);
    }
    
    
};

解法2:层次遍历+hashmap

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /*
     * @param : the root of tree
     * @param : the target sum
     * @return: two number from tree witch sum is n
     */
    vector<int> twoSum(TreeNode * root, int n) {
        if (!root) return {};
        unordered_set<int> us;
        queue<TreeNode *> q;
        vector<int> res;
        q.push(root);
        us.insert(root->val);
        while (!q.empty()) {
            TreeNode *frontNode = q.front();
            q.pop();
            if (us.find(n - frontNode->val) != us.end()) {
                return {frontNode->val, n - frontNode->val};
            }
            if (frontNode->left) {
                q.push(frontNode->left);
                us.insert(frontNode->left->val);
            }
            if (frontNode->right) {
                q.push(frontNode->right);
                us.insert(frontNode->right->val);
            }
        }
        return {};
    }
};
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值