本周学习了哈希、树、堆、图
简单:
中等:
- 字母异位词分组(亚马逊在半年内面试中常考)
- 二叉树的中序遍历(亚马逊、字节跳动、微软在半年内面试中考过)
- 二叉树的前序遍历(字节跳动、谷歌、腾讯在半年内面试中考过)
- N叉树的层序遍历(亚马逊在半年内面试中考过)
- 丑数(字节跳动在半年内面试中考过)
- [前K个高频元素(亚马逊在半年内面试中常考)]
- 哈希相关:
- [找雪花]
- 树相关:
- [安装路灯]
- 二叉搜索树相关:
- [二叉搜索树的后序遍历序列]
- 堆相关:
- [最火视频榜单]
- 图相关:
- [手游上线]
-
本周可以沉淀的代码
- 二叉树遍历(递归)
// 前序遍历核心代码 public void preorder(TreeNode root, List<Integer> list) { if (root == null) { return; } list.add(root.val); preorder(root.left, list); preorder(root.right, list); } // 中序遍历核心代码 public void inorder(TreeNode root, List<Integer> list) { if (root == null) { return; } inorder(root.left, list); list.add(root.val); inorder(root.right, list); } // 后续遍历核心代码 public void postorder(TreeNode root, List<Integer> list) { if (root == null) { return; } postorder(root.left, list); postorder(root.right, list); list.add(root.val); }- 二叉树遍历(迭代)
// 前序遍历核心代码 while(root != null || !stack.isEmpty()){ //go left down to the ground while(root != null){ res.add(root.val); stack.push(root); root = root.left; } //if we reach to the leaf, go back to the parent right, and repeat the go left down. TreeNode cur = stack.pop(); root = cur.right; } // 中序遍历核心代码 while (root != null || !stack.isEmpty()) { while (root != null) { stack.push(root); root = root.left; } root = stack.pop(); res.add(root.val); root = root.right; } // 后续遍历核心代码,前序遍历的一个逆序 while(root != null || !stack.isEmpty()){ while(root != null){ res.add(root.val); stack.push(root); root = root.right; } TreeNode cur = stack.pop(); root = cur.left; } Collections.reverse(res); -
本周学习心得