首页 > 其他分享 >树的遍历——迭代

树的遍历——迭代

时间:2023-09-05 10:44:50浏览次数:27  
标签:遍历 迭代 list List new null root stack

/*中序遍历*/

class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list=new ArrayList<>(); Deque<TreeNode> stack=new LinkedList<>(); while(root!=null || !stack.isEmpty()){ while(root!=null){ stack.push(root); root=root.left; } root=stack.pop(); list.add(root.val); root=root.right; } return list; } }
/*前序遍历*/

class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list=new ArrayList<>(); Deque<TreeNode> stack=new LinkedList<>(); while(!stack.isEmpty() || root!=null){ while(root!=null){ list.add(root.val); stack.push(root); root=root.left; } root=stack.pop(); root=root.right; } return list; } }
/*后序遍历*/

class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); if (root == null) { return res; } Deque<TreeNode> stack = new LinkedList<TreeNode>(); TreeNode prev = null; while (root != null || !stack.isEmpty()) { while (root != null) { stack.push(root); root = root.left; } root = stack.pop(); if (root.right == null || root.right == prev) { res.add(root.val); prev = root; root = null; } else { stack.push(root); root = root.right; } } return res; } }

 

标签:遍历,迭代,list,List,new,null,root,stack
From: https://www.cnblogs.com/ztzzh-1/p/17679033.html

相关文章

  • Python 遍历字典的若干方法
    哈喽大家好,我是咸鱼我们知道字典是Python中最重要且最有用的内置数据结构之一,它们无处不在,是语言本身的基本组成部分我们可以使用字典来解决许多编程问题,那么今天我们就来看看如何在Python中遍历字典全文内容:https://realpython.com/iterate-through-dictionary-python/p......
  • 泛微E-cology filedownload目录遍历漏洞
    漏洞描述泛微E-cologyfiledownload文件存在目录遍历漏洞漏洞复现fofa查询语法:app="泛微-协同办公OA"鹰图查询语法:app.name="泛微e-cology9.0OA"登录页面如下:POC:/weaver/ln.FileDownload?fpath=../ecology/WEB-INF/web.xmlnuclei批量yaml文件id:ecology-filedownloa......
  • 用递归和非递归两种方式实现二叉树的先序遍历(前序遍历)
    一、分析先序遍历(前序遍历)遍历顺序为:根、左、右。二、递归实现publicclassNode{ publicintvalue;publicNodeleft;publicNoderight;publicNode(intdata){ this.value=data;}}publicvoidpreOrderRecur(Nodehead){ if(head==null){ return......
  • HashMap遍历方式
    HashMap是一个键值对的集合,我们不能通过简单的循环来遍历HashMap,所以我们一般通过以下两种方式来遍历HashMap,一种是通过KeySet集合来遍历,另一种是通过entry键值对对象来遍历。KeySet遍历HashMap通过keySet()方法获取HashMap的keySet集合遍历keySet集合,可以使用iterator迭代器或......
  • 设计模式-迭代器
    文章目录1.引言1.1概述1.2设计模式1.3迭代器模式的应用场景1.4迭代器模式的作用2.基本概念2.1迭代器Iterator2.2聚合Aggregate2.3具体聚合ConcreteAggregate3.Java实现迭代器模式3.1Java集合框架3.2Java迭代器接口3.3Java迭代器模式实现示例4.迭代器模式的优......
  • 遍历技巧
    菜鸟教程:https://www.runoob.com/python3/python3-data-structure.html链接  1#在字典中遍历时,关键字和对应的值可以使用items()方法同时解读出来:2knights={'gallahad':'thepure','robin':'thebrave'}3fork,vinknights.items():4......
  • 设计模式:迭代器模式
    设计模式wiki中将设计模式分为四类,分别是:创建模式(creationalpatterns)结构模式(structuralpatterns)行为模式(behavioralpatterns)并发模式(concurrencypatterns)迭代器模式属于其中的行为模式。迭代器做的只有一件事,就是解决集合的遍历问题。以下是wiki对迭代器模式的概括......
  • 优化函数迭代每次都要查找的代码
    在做Perfeye需求的时候,有写了一个函数,每次遍历,要根据相同的时间,把对应的数据整合刚开始用findIndex进行每次查找,但是性能很差,后面问了gpt有没有什么操作能不用findIndex,gpt说可以单独存储时间索引值来进行判断旧代码constgetFTimeDataSetSourceData=()=>{constresu......
  • Vue+Elemnt-UI遍历生成form-item并为其绑定校验规则
    需求:接口获取数据,动态渲染表单(文本框类型,内容,标签,是否必填)参照博主:blog.csdn.net/qq_33769914/article/details/122449601遇到的问题:1.通过对单个item绑定的校验规则不生效(表现为:不弹提示,或填了内容依旧提示)           2.提示出现后通过clearValidate()......
  • js 遍历json格式数据到table中
    前端aspx文件,js把数据显示在表格中1.首先我们在前端页面写一个table代码。1<tableborder="1"id="gystable"cellpadding="0"cellspacing="0"class="frame5">2......