国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode 4. Median of Two Sorted Arrays(二叉搜索樹的使用)

2019-11-08 03:04:34
字體:
來源:轉載
供稿:網友

題意: 給出2個有序的數組,找出2個數組合并后的中位數,直接合并沒嘗試, 寫了個二叉搜索樹,直接遍歷一遍,找到數就好了, 恩 代碼寫的像屎一樣。

struct node{ int data; node *l,*r; node(){l=r=NULL;} node(int t):data(t){l=r=NULL;}};void insert(node* &root, int val){ if(!root){ root = new node(val); return ; } if(val < root->data){ insert(root->l, val); } else { insert(root->r, val); }}int cnt=0,p1,p2,x,y;double ans;void show(const node* root){ if(!root){ return ; } if(root->l){ show(root->l); } if(cnt==p1)x=root->data; if(cnt==p2)y=root->data;cnt++; if(root->r){ show(root->r); }}class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { node *root=NULL; for(int i=0;i<nums1.size();i++){ insert(root, nums1[i]); } for(int i=0;i<nums2.size();i++){ insert(root, nums2[i]); } cnt=0;int sum = nums1.size()+nums2.size(); p1 = sum/2-1; p2=sum/2; show(root); if(sum&1){ ans = y; } else { ans = (x+y)/2.0; } return ans; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和硕县| 张家港市| 彩票| 荔波县| 遂溪县| 大田县| 盘山县| 中超| 大城县| 乐陵市| 毕节市| 涡阳县| 滦平县| 姜堰市| 抚顺县| 福海县| 鹤岗市| 碌曲县| 莎车县| 越西县| 仙居县| 贡觉县| 阿勒泰市| 永新县| 军事| 石泉县| 定结县| 宜章县| 平塘县| 绥德县| 玉山县| 松溪县| 南通市| 清徐县| 信宜市| 汕尾市| 尉氏县| 镇平县| 天全县| 信宜市| 天全县|