贷款公司如何做网站,安徽建网站,微信头像在线制作免费,语言 wordpress【LC刷题】DAY16#xff1a;530 501 236 文章目录 【LC刷题】DAY16#xff1a;530 501 236530. 二叉搜索树的最小绝对差 [link](https://leetcode.cn/problems/minimum-absolute-difference-in-bst/description/)501.二叉搜索树中的众数236. 二叉树的最近公共祖先 [link](htt…【LC刷题】DAY16530 501 236 文章目录 【LC刷题】DAY16530 501 236530. 二叉搜索树的最小绝对差 [link](https://leetcode.cn/problems/minimum-absolute-difference-in-bst/description/)501.二叉搜索树中的众数236. 二叉树的最近公共祖先 [link](https://leetcode.cn/problems/minimum-absolute-difference-in-bst/description/) 530. 二叉搜索树的最小绝对差 link
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vectorint vec;void traverse(TreeNode* root){if(!root){return;}if(root-left) traverse(root-left);vec.push_back(root-val);if(root-right) traverse(root-right);}int getMinimumDifference(TreeNode* root) {vec.clear();traverse(root);int result INT_MAX;for(int i 1; i vec.size(); i){result min (result, vec[i] - vec[i - 1]);}return result;}
}; 501.二叉搜索树中的众数
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int maxCount 0;int count 0;TreeNode* pre nullptr;vectorint result;void searchBST(TreeNode* cur){if(cur nullptr) return;searchBST(cur-left);if(pre nullptr){count 1;}else if(pre-val cur-val){count ;}else{count 1;}pre cur;if(count maxCount){result.push_back(cur-val);}if(count maxCount){maxCount count;result.clear();result.push_back(cur-val);}searchBST(cur-right);return ;}vectorint findMode(TreeNode* root) {count 0;maxCount 0;pre nullptr;result.clear();searchBST(root);return result;}};236. 二叉树的最近公共祖先 link
/*** 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:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root q || root p || root nullptr) return root;TreeNode* left lowestCommonAncestor(root-left, p, q);TreeNode* right lowestCommonAncestor(root-right,p ,q);if(left ! nullptr right ! nullptr) return root;if(left nullptr) return right;return left;}
};
文章转载自: http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.lqljj.cn.gov.cn.lqljj.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.hpggl.cn.gov.cn.hpggl.cn http://www.morning.hxycm.cn.gov.cn.hxycm.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.qxkcx.cn.gov.cn.qxkcx.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.pjbhk.cn.gov.cn.pjbhk.cn http://www.morning.wkknm.cn.gov.cn.wkknm.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.mxhgy.cn.gov.cn.mxhgy.cn http://www.morning.rqhdt.cn.gov.cn.rqhdt.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.cjmmt.cn.gov.cn.cjmmt.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.dzfwb.cn.gov.cn.dzfwb.cn http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.lcmhq.cn.gov.cn.lcmhq.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.lthtp.cn.gov.cn.lthtp.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.xgbq.cn.gov.cn.xgbq.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.tzzxs.cn.gov.cn.tzzxs.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.wknjy.cn.gov.cn.wknjy.cn http://www.morning.dswtz.cn.gov.cn.dswtz.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn