diff --git a/code_segment/shili.cpp b/code_segment/shili.cpp index bfe37026..d00c5919 100644 --- a/code_segment/shili.cpp +++ b/code_segment/shili.cpp @@ -1,5 +1,28 @@ #include using namespace std; + +class Wood{ +private: + int length; + int width; +public: + int get_length(){ + return this->length; + } + int get_width(){ + return this->width; + } +}; + +class desk:public wood{ +private: + int height; +public: + int get_height(){ + return this->height; + } +}; + int add(int a,int b){ int c = a+b; return c; diff --git a/工作日志/2021年9月12日.md b/工作日志/2021年9月12日.md new file mode 100644 index 00000000..4ddef2c7 --- /dev/null +++ b/工作日志/2021年9月12日.md @@ -0,0 +1 @@ +1. 关于封装的讨论。 \ No newline at end of file diff --git a/数据结构/6.1 二叉树.md b/数据结构/6.1 二叉树.md index a5303792..467f0dd3 100644 --- a/数据结构/6.1 二叉树.md +++ b/数据结构/6.1 二叉树.md @@ -28,7 +28,7 @@ ### 完全二叉树 -* 如果所有叶子都位于相同的水平d,则称二元树是完全二叉树。完全二叉树是二叉树,在0级和d级之间的每个级别包含正好$2 ^d$个节点。 具有深度d的完全二叉树中的节点总数是$2^{(d+1)}-1$,其中叶节点是$2^d&,非叶节点是$2^d-1$。 +* 如果所有叶子都位于相同的水平d,则称二元树是完全二叉树。完全二叉树是二叉树,在0级和d级之间的每个级别包含正好$2^d$个节点。 具有深度d的完全二叉树中的节点总数是$2^{(d+1)}-1$,其中叶节点是$2^d$,非叶节点是$2^d-1$。 ![](image/2021-03-12-22-12-52.png) diff --git a/数据结构/6.1.cpp b/数据结构/6.1.cpp index a14d7192..8b4d34fd 100644 --- a/数据结构/6.1.cpp +++ b/数据结构/6.1.cpp @@ -134,6 +134,39 @@ public: // 最后肯定是空元素组成的队列,创建一个额外的空数组。 vec.pop_back(); return vec; + } + // 区分层次的层序遍历2 改进版,单队列,单循环 + vector> levelOrder2(TreeNode* root) { + vector> res; + vector vec; + queue que; + que.push(root); + int N=1; + + while(!que.empty()){ + // 每次交换队列创建一个数组 + if(que.front()==nullptr){ + que.pop(); + N--; + continue; + } + + TreeNode*temp=que.front(); + que.pop(); + vec.push_back(temp->val); + N--; + + que.push(temp->left); + que.push(temp->right); + //可以在这里针对每层进行操作。例如反转。 + if(N==0){ + res.push_back(vec); + vec.clear(); + N=que.size(); + } + } + + return res; } //重建二叉树-前中 /* @@ -174,7 +207,7 @@ int main(){ TreeNode n; TreeNode* node=&n; BinaryTree tree; - // tree.build(node,vec,0); + tree.build(node,vec,0); // tree.display(node); // cout<> res = tree.levelOrder2(node); + for(auto a:res){ + for(auto b:a){ + cout< pre{3,9,20,15,7}; - vector mid{9,3,15,20,7}; - TreeNode* node2 = tree.rebuild(pre,mid,0,0,mid.size()-1); - tree.display(node2); - return 0; + // vector pre{3,9,20,15,7}; + // vector mid{9,3,15,20,7}; + // TreeNode* node2 = tree.rebuild(pre,mid,0,0,mid.size()-1); + // tree.display(node2); + // return 0; } \ No newline at end of file