递归|迭代的前序遍历

  • 前言
  • 一、扁平化嵌套列表迭代器
  • 二、前序遍历(无需访问非叶节点)
    • 1、递归实现
    • 2、栈模拟
    • 3、惰性栈模拟(next时再入栈)
  • 总结
  • 参考文献

前言

问题转换:将需求确认,结合已学知识,进行匹配->改进。列表的扁平化本质就是树的前序遍历,不过不需要父节点而已。树的前序遍历可以递归实现,也可以迭代实现,一题多解,方便理解树的递归结构,前序遍历的多递归本质!才能对里面的知识点融汇贯通,举一反三!

一、扁平化嵌套列表迭代器

二、前序遍历(无需访问非叶节点)

1、递归实现

// 扁平化嵌套列表迭代器。
public class NestedIterator implements Iterator<Integer> {List<Integer> arr = new ArrayList<>();int idx = 0;// 层层嵌套,就像是学过的树结构,integer为叶子节点,list为父节点。// 扁平化的过程就像是前序遍历一样。public NestedIterator(List<NestedInteger> nestedList) {// 递归拿元素。dfs(nestedList);}// 树的前序遍历。private void dfs(List<NestedInteger> nestedList) {// 多个平行递归,树就是多支子树递归结构。for (NestedInteger nest : nestedList) {if (nest.isInteger()) arr.add(nest.getInteger());else dfs(nest.getList());}}@Overridepublic boolean hasNext() {return idx < arr.size();}@Overridepublic Integer next() {int val = idx < arr.size() ? arr.get(idx) : -1;if (idx < arr.size()) ++idx;return val;}class NestedInteger {List<NestedInteger> nestedList;Integer val;public NestedInteger(List<NestedInteger> nestedList, Integer val) {this.nestedList = nestedList;this.val = val;}public boolean isInteger() {return val != null;}public List<NestedInteger> getList() {return nestedList;}public Integer getInteger() {return val;}}
}

2、栈模拟

// 迭代 + 栈 实现。
class NestedIterator2 implements Iterator<Integer> {List<Integer> arr = new ArrayList<>();int idx = 0;// 层层嵌套,就像是学过的树结构,integer为叶子节点,list为父节点。// 扁平化的过程就像是前序遍历一样。public NestedIterator2(List<NestedInteger> nestedList) {// 根左右Stack<NestedInteger> sk = new Stack<>();for (int i = nestedList.size() - 1; i >= 0; i--) sk.push(nestedList.get(i));NestedInteger root;while (!sk.isEmpty()) {root = sk.pop();if (root.isInteger()) arr.add(root.getInteger());else {List<NestedInteger> list = root.getList();for (int i = list.size() - 1; i >= 0; i--) sk.push(list.get(i));}}}@Overridepublic boolean hasNext() {return idx < arr.size();}@Overridepublic Integer next() {int val = idx < arr.size() ? arr.get(idx) : -1;if (idx < arr.size()) ++idx;return val;}class NestedInteger {List<NestedInteger> nestedList;Integer val;public NestedInteger(List<NestedInteger> nestedList, Integer val) {this.nestedList = nestedList;this.val = val;}public boolean isInteger() {return val != null;}public List<NestedInteger> getList() {return nestedList;}public Integer getInteger() {return val;}}
}

3、惰性栈模拟(next时再入栈)

// 迭代 + 栈 + next时访问元素。
class NestedIterator3 implements Iterator<Integer> {NestedInteger root;Stack<NestedInteger> sk = new Stack<>();// 层层嵌套,就像是学过的树结构,integer为叶子节点,list为父节点。// 扁平化的过程就像是前序遍历一样。public NestedIterator3(List<NestedInteger> nestedList) {// 根左右for (int i = nestedList.size() - 1; i >= 0; i--) sk.push(nestedList.get(i));}@Overridepublic boolean hasNext() {while (!sk.isEmpty() && !sk.peek().isInteger()) {root = sk.pop();List<NestedInteger> list = root.getList();for (int i = list.size() - 1; i >= 0; i--) sk.push(list.get(i));}return !sk.isEmpty();}@Overridepublic Integer next() {return sk.pop().getInteger();}class NestedInteger {List<NestedInteger> nestedList;Integer val;public NestedInteger(List<NestedInteger> nestedList, Integer val) {this.nestedList = nestedList;this.val = val;}public boolean isInteger() {return val != null;}public List<NestedInteger> getList() {return nestedList;}public Integer getInteger() {return val;}}
}

总结

1)问题转换,确认需求,建模(抽象出来),利用已学知识解决(一般需需要变形,或增加一些细节。),是常见的问题解决方案。当然如果问题转换比较巧妙,则属于脑筋急转弯!

2)树本身就是递归结构,所以树的前序遍历就是多递归,也可以理解为,递归开辟for循环,循环来访问每棵子树。

3)一题多解,深刻理解树的递归结构,前序遍历的本质,才能对考察的知识点融汇贯通,进而举一反三!

参考文献

[1] LeetCode 扁平化嵌套列表迭代器

扁平化嵌套列表迭代器 [树的递归前序遍历 + 迭代前序遍历]相关推荐

  1. 力扣--扁平化嵌套列表迭代器

    扁平化嵌套列表迭代器 文章目录 扁平化嵌套列表迭代器 一.题目描述 二.分析 方法一: 代码一: 方法二: 代码二: C++代码: 一.题目描述 /*** // This is the interfa ...

  2. 341. 扁平化嵌套列表迭代器

    2020-05-24 1.题目描述 扁平化嵌套列表迭代器 2.题解 使用递归即可 3.代码 /*** // This is the interface that allows for creating ...

  3. leetcode 341. Flatten Nested List Iterator | 341. 扁平化嵌套列表迭代器(Java)

    题目 https://leetcode.com/problems/flatten-nested-list-iterator/ 这标题,翻译的是人话吗?啥叫扁平化嵌套列表迭代器?.. 题解 比较像深度优 ...

  4. leetcode 341. 扁平化嵌套列表迭代器(dfs)

    给你一个嵌套的整型列表.请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的每一项或者为一个整数,或者是另一个列表.其中列表的元素也可能是整数或是其他列表. 示例 1: 输入: [[1 ...

  5. 每日一题:leetcode341.扁平化嵌套列表迭代器

    题目描述 题目分析 这个题目自己大概花了一个小时,虽然是一遍AC,但是速度有点慢,太长时间不写代码导致自己对代码不太敏感,写起来慢腾腾的. 看到这个的想法就是,要用栈来保存列表的迭代器,这样将孩子列表 ...

  6. LeetCode 341. 扁平化嵌套列表迭代器(双栈)

    文章目录 1. 题目 2. 双栈解题 1. 题目 给定一个嵌套的整型列表.设计一个迭代器,使其能够遍历这个整型列表中的所有整数. 列表中的项或者为一个整数,或者是另一个列表. 示例 1: 输入: [[ ...

  7. leetcode算法题--扁平化嵌套列表迭代器

    题目链接:https://leetcode-cn.com/problems/flatten-nested-list-iterator/ 这个题目不难,就是像树的深度遍历,就是题目有点难理解. /*** ...

  8. 树形json扁平化,一维数组树状化,对象深拷贝,元素后插入新元素,格式或动态路由等常用js合集

    索引 一.在元素后面插入一个新的元素. 二.对象或者数组的深拷贝. 三.从服务器端获取到动态路由表的格式化. 四.json树形数据扁平化处理(变成一维数组) 五.一维数组转化为树状结构对象. 六.防抖 ...

  9. python列表遍历 空列表_Python list列表执行reversed()后执行pop()返回迭代对象遍历为空问题...

    1.示例代码 1)一般情况s = [1,2,3] t = reversed(s) for i in t: print(i) # 输出: 3,2,1 2) 执行pop()s = [1,2,3] t = ...

最新文章

  1. 【ArcGIS for Android】基于位置查询Graphic和Feature
  2. 记一次数据库崩溃的恢复
  3. Apache - AH00526 – server.crt
  4. 技术债务就像俄罗斯方块,你永远都赢不了!
  5. ASN.1 Editor
  6. Hyperledger Fabric 1.0 实例简析 第一课 network_setup.sh分析
  7. 除了超分辨率,AI 结合 RTC 还有哪些技术实践?
  8. js笔记(五)文档对象模型DOM
  9. matlab 直方图 肥尾,概率分布细谈:厚尾、长尾、幂律、指数
  10. android uri跳转导航,android:scheme 通过uri跳转到APP应用指定Activity
  11. showdoc修改json转表格格式
  12. Android购物商城项目
  13. 【转载】Web前端框架图
  14. AJRW错误AA707
  15. Numpy 中某个矩阵的平方距离计算方法
  16. 无法打开匿名级安全令牌解决方法
  17. wow服务器人数最新统计,魔兽世界怀旧服服务器人数统计 魔兽世界怀旧服人数比例查询...
  18. 在sql 2000中实现Oracle 中 rownum的功能
  19. 星座图中格雷映射及其实现
  20. 如何看待网易强制取消所有用户魔兽世界游戏时间

热门文章

  1. 电信 802.1p 设置_手机SIM卡怎么设置密码
  2. 【java学习】EasyExcel的简单使用
  3. creator Label竖行显示
  4. Build.VERSION.SDK_INT = Build.VERSION_CODES.HONEYCOMB
  5. 网络共享计算机没有密码登不聊,局域网别人无法打开我的共享文档(要密码,可是我没设),怎么解决?...
  6. 发票专用驱动sjz_“数智企业财税云领”增值税专用发票主题交流会圆满举办| 从专票电子化开始 开启企业数智化之旅...
  7. 建筑防火在民用住宅建筑设计中的应用
  8. 华为p9总是显示切换服务器中,原来华为手机是有双系统模式的,保护个人隐私,一部手机相互切换!...
  9. 3dmax中如何隐藏骨骼
  10. 3dMax先蒙皮刷权重,再附加合并