LeetCode – Construct Binary Tree from Inorder and Postorder Traversal

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given 1 2 inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 1 2 3 4 5 3 / \ 9 20 / \ 15 7 实现: 递归方式,根据二叉树中序遍历和后续遍历,创建二叉树。中序遍历: 左-根-右, 后续遍历: 左-右-根, 所以可以通过后续遍历的最后一个节点找到根节点,并生成根节点, 因为二叉树没有重复节点, 就 可以在中序遍历数据中找到根节点,再分成左右两个子树,递归执行下去,遍历完所有节点。 通过postorder获取根节点, 通过inorder生成左右子树,查找节点在中序遍历数据组里位置,可以通过unordered_map实现。 不使用map情况下 时间复杂度 O(N^2) , 使用map查找O(N) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 /** * 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: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { unordered_map <int, int> treemap; for (int i=0; i<inorder.size();i++) { treemap[inorder[i]] = i; } int index = inorder.size()-1; return helper(inorder, postorder, 0, inorder.size()-1, &index, treemap); } TreeNode *helper(vector<int> & inorder, vector<int>& postorder, int left, int right, int *index , unordered_map<int, int>&treemap){ //check if (left > right){ return NULL; } // create root node /* destory postorder vector int val = postorder.back(); TreeNode *node = new TreeNode(val); postorder.pop_back(); */ int val = postorder[*index]; TreeNode *node = new TreeNode(val); (*index)--; // search inorder by array /* int root = -1; for (int i=left ; i <=right; i ++){ if (val == inorder[i]){ root = i; break; } } if (root == -1){ return node; }*/ // search inorder by map int root = treemap[val]; // create left & right node node->right = helper(inorder, postorder, root+1, right, index, treemap); node->left = helper(inorder, postorder, left, root-1, index, treemap); return node; } };

2020-07-25 · 2 min · 341 words · Garlic Space

Why mmap is faster than system calls

原文链接 作者 Alexandra Sasha Fedorova

2020-07-24 · 1 min · 50 words · Garlic Space

mmap相关API

mmap调用进程的虚拟进程空间中一段新的内存映射。

2020-07-22 · 5 min · 893 words · Garlic Space

centos7 升级 glibc && gcc

cenos7X86_64

2020-07-18 · 3 min · 527 words · Garlic Space

cenos7+openssh 修改SSH服务端口

环境: cenos7(X86_64) + openssh ssh端口配置 修改端口 例如8888 文件 /etc/ssh/sshd_config 1 2 #Port 22 Port 8888 重启服务 1 2 systemctl restart sshd systemctl status sshd 防火墙配置: 我这里配置比较简单仅zone配置了一个public 1 firewall-cmd --permanent --zone=public --add-port=8888/tcp 重启服务 1 2 systemctl restart firewalld systemctl status firewalld

2020-07-13 · 1 min · 40 words · Garlic Space

LeetCode – Path Sum Solution

题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 1 2 3 4 5 6 7 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22. ...

2020-07-12 · 2 min · 250 words · Garlic Space

LeetCode – Symmetric Tree Solution

题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 2 3 4 5 1 / \ 2 2 / \ / \ 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 2 3 4 5 1 / \ 2 2 \ \ 3 3 Follow up: Solve it both recursively and iteratively. ...

2020-07-12 · 2 min · 279 words · Garlic Space

Linux slob/slab/slub

Linux初始化通过bootmem/memblock引导内存分配进行内存管理,支持buddy system完成相关初始化后,将物理内存分配的功能转交给buddy system,buddy system是以page为单位分配方式。 对于内核要经常创建的对象, 如task_struct,fs_struct, mm_struct

2020-07-11 · 8 min · 1562 words · Garlic Space

为什么x86无法生存-Why x86 won’t survive

前不久, 评估CEO 库克在其全球开发者大宣布,Mac笔记本以及个人电脑将会改用苹果自家的ARM架构处理器。这将是自2006年从PowerPC处理器改用英特尔x86处理器后,又一次CPU架构的调整。下面这篇文章是Medium推送的,不过文章贬低X86中提到的漏洞, 我查了资料其实部分ARM也是存在的。 不过确实是代表了一类观点。 苹果

2020-06-29 · 1 min · 59 words · Garlic Space

通过alternatives进行多版本间切换

上周在学习单点登录安装CAS Overlay Template要使用指定版本,当时机器上安装了多个版本的jdk使用alternatives命令进行了切换

2020-06-28 · 2 min · 393 words · Garlic Space