在二叉搜索树里,寻找两个节点 Find a pair with given sum in a Balanced BST
来源:互联网 发布:无锡大数据会议 编辑:程序博客网 时间:2024/06/07 16:48
在二叉搜索树里,寻找两个节点 Find a pair with given sum in a Balanced BST
给定一个平衡二叉搜索树和一个目标和sum,写一个函数,如果有一对节点的和等于目标和, 返回true,否则返回false。预期时间复杂度是O(n),并只只能有O(logN)的额外的空间可以使用。修改二叉搜索树是不允许的。注意平衡BST的身高是O(LOGN)。
一个解决办法是创建BST的中序遍历序列数组。一旦我们有了中序遍历序列数组,我们可以在O(n)时间找到那两个元素。该解决方案在O(n)的时间,但需要O(n)的辅助空间。
还有一个方法,同时进行中序遍历和逆中序遍历,遍历的时候计算两个节点的和,如果节点的和大于目标值,那么逆中序遍历下一个节点,否则,中序遍历下一个节点。注意,这里我们只能用非递归的方法进行中序遍历和逆中序遍历,这样才能中止中序遍历(逆中序遍历)转而进行逆中序遍历(中序遍历)。
非递归中序遍历参考:http://blog.csdn.net/jiyanfeng1/article/details/41529321
本问题的代码如下:
#include<iostream>#include<stdio.h>#include<stdlib.h>using namespace std;#include <string.h>#include <stack>#include <stdio.h>#include <stdlib.h>struct Node{ int data; Node* left; Node* right; Node(int k, Node* l, Node* r): data(k), left(l), right(r){};};stack<Node*> st;void pairSum(Node* root1, stack<Node*> st1, Node* root2, stack<Node*> st2, int sum){int flag1 = 1; // when flag1=1, in-order traversal is allowed.int flag2 = 1; // when flag2=1, reserver in-order traversal is allowed. // At the begining, flag1=1 and flag2=1 so that we // can find the left-most and right-most nodeint data1, data2; // data1 is the current node value from in-order traversal // data2 is the current node value from reverse in-order traversalwhile(1){while(root1 && flag1){st1.push(root1);root1 = root1->left;}while(root2 && flag2){st2.push(root2);root2 = root2->right;}if(st1.size()>0 || st2.size()>0 ){if(flag1 && st1.size()>0){root1 = st1.top();st1.pop();data1 = root1->data;}if(flag2 && st2.size()>0){root2 = st2.top();st2.pop();data2 = root2->data;}if(data1 + data2 == sum){cout<<data1<<" "<<data2<<endl;return;}else if (data1 + data2 < sum){flag1 = 1;flag2 = 0;root1 = root1->right;}else{flag1 = 0;flag2 = 1;root2 = root2->left;}}elsebreak;}}int main(){ Node* n1 = new Node(1,NULL,NULL); Node* n3 = new Node(3,NULL,NULL); Node* n2 = new Node(2,n1,n3); Node* n5 = new Node(5,NULL,NULL); Node* n4 = new Node(4,n2,n5); Node* n0 = new Node(0,NULL,n4); /* Constructed binary tree is 0 \ 4 / \ 2 5 / \ 1 3 */ int sum = 1;stack<Node*> st1, st2;pairSum(n0, st1, n0, st2, sum);}
0 0
- 在二叉搜索树里,寻找两个节点 Find a pair with given sum in a Balanced BST
- find all pairs of elements in a balanced BST that sum to a certain number
- 【BST】在搜索二叉树中,寻找节点数据域在[L,U]范围内的节点
- *1115. Counting Nodes in a BST (30) <二叉搜索树>
- pat甲1115. Counting Nodes in a BST(BST二叉搜索树)
- leetcode 230. Kth Smallest Element in a BST 二叉搜索树BST的中序遍历
- leetcode 450. Delete Node in a BST 二叉搜索树BST删除结点
- 二叉树-Largest BST in a tree
- CareerCup Find the ceiling value present in the BST of a given ke
- 【二叉树】二分查找树,节点删除【Add to List 450. Delete Node in a BST】
- PAT 1115. Counting Nodes in a BST (30) 搜索树建立 + 各层节点数量判断
- 【LeetCode450】Delete Node in BST二叉搜索树删除节点
- Kth Smallest Element in a BST 找二叉搜索树中的第k小的元素
- 230.leetcode Kth Smallest Element in a BST(medium)[二叉搜索树 先序遍历 栈]
- 【二叉搜索树:删除指定结点】leetcode 450. Delete Node in a BST
- 1115. Counting Nodes in a BST (30)(建立二叉搜索树)
- No. 09 - Numbers with a Given Sum
- 【二叉树】寻找二叉树/BST的两个节点的最低公共祖先
- ZAM 3D入门教程(6):导入AI和EPS格式的文件
- dll工程lib库的生成问题
- ASP中的时间函数
- SWT基本类说明
- php与mysql连接
- 在二叉搜索树里,寻找两个节点 Find a pair with given sum in a Balanced BST
- 4.4 task_struct结构在内存中的存放 4.5进程组织的方式
- web.xml配置
- VC的MFC工程加载GIF格式动态图片
- js 写程序日志代码
- Xcode6 中一些设置
- 基于MySQL游标的具体使用详解
- unity3d android工程签名
- BMP 转 YUV (BMP2YUV)+ YUV 转RGB