LeetCode Tree

LeetCode + 《剑指Offer II》刷题笔记。 树 二叉树的DFS Preorder Traversal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> s; TreeNode* cur=root; while(cur || !s.empty()){ while(cur){ res.push_back(cur->val); s.push(cur); cur=cur->left; } cur=s.top();

《纳瓦尔宝典》

豆瓣🔗:《纳瓦尔宝典》 读过这本书的中文版后,更推荐英文原版阅读,比较简单。 2022的很多时候想起2021甚至2020的困窘,觉得自己好愚蠢,

LeetCode Sort

LeetCode + 《剑指Offer II》刷题笔记。 排序 Interval 56 Merge Intervals 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public: vector<vector<int>> merge(vector<vector<int>>& intervals) { sort(intervals.begin(),intervals.end()); vector<vector<int>> res; //avoid an edge case res.push_back(intervals[0]); for(int i=1;i<intervals.size();i++){ int start=intervals[i][0],end=intervals[i][1]; int lastEnd=res[res.size()-1][1]; if(start<=lastEnd){ //the reason of