給定一個二叉搜索樹的 根節點 root 和一個整數 k , 請判斷該二叉搜索樹中是否存在兩個節點它們的值之和等于 k 。假設二叉搜索樹中節點的值均唯一。
class Solution {
public:bool findTarget(TreeNode* root, int k) {unordered_set<int> ret;TreeNode *curr=root;stack<TreeNode*> st;while(!st.empty()||curr!=NULL){while(curr!=NULL){st.push(curr);curr=curr->left;}curr=st.top();st.pop();if(ret.count(k-curr->val)) return true;ret.insert(curr->val);curr=curr->right;}return false;}
};
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态