树的遍历算法题总结(第二十六天)

2024-04-19 13:44

本文主要是介绍树的遍历算法题总结(第二十六天),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

144. 二叉树的前序遍历

题目

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

答案

class Solution {List<Integer> res = new ArrayList(); public List<Integer> preorderTraversal(TreeNode root) {deal(root);return res;}void deal(TreeNode root){if(root==null){return;}res.add(root.val);deal(root.left);deal(root.right);}
}class Solution {List<Integer> res = new ArrayList();public List<Integer> preorderTraversal(TreeNode root) {if(root==null){return res;}Stack<TreeNode> stack = new Stack();stack.push(root);while(!stack.isEmpty()){TreeNode curr = stack.pop();res.add(curr.val);if(curr.right!=null) stack.push(curr.right);//先放右再放左if(curr.left!=null) stack.push(curr.left);}return res;}
}








94. 二叉树的中序遍历

题目

给定一个二叉树的根节点 root ,返回 它的 中序 遍历

答案

class Solution {List<Integer> res = new ArrayList();public List<Integer> inorderTraversal(TreeNode root) {deal(root);return res;}void deal(TreeNode root){if(root==null){return;}deal(root.left);res.add(root.val);deal(root.right);}
}








145. 二叉树的后序遍历

题目

给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历

答案

class Solution {List<Integer> res = new ArrayList();public List<Integer> postorderTraversal(TreeNode root) {deal(root);return res;}void deal(TreeNode root){if(root==null){return;}deal(root.left);deal(root.right);res.add(root.val);}
}class Solution {List<Integer> res = new ArrayList();public List<Integer> postorderTraversal(TreeNode root) {if(root==null){return res;}Stack<TreeNode> stack = new Stack();stack.push(root);while(!stack.isEmpty()){TreeNode curr = stack.pop();res.add(curr.val);if(curr.left!=null) stack.push(curr.left);if(curr.right!=null) stack.push(curr.right);}Collections.reverse(res);return res;}
}







102. 二叉树的层序遍历

题目

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

答案

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList();if(root==null){return res;}Queue<TreeNode> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> list = new ArrayList();for(int i=0;i<size;i++){TreeNode curr = queue.poll();list.add(curr.val);if(curr.left!=null) queue.offer(curr.left);if(curr.right!=null) queue.offer(curr.right);}res.add(list);}return res;}
}







107. 二叉树的层序遍历 II

题目

给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

答案

class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> res = new LinkedList();if(root==null){return res;}Queue<TreeNode> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> list = new ArrayList();for(int i=0;i<size;i++){TreeNode curr = queue.poll();list.add(curr.val);if(curr.left!=null) queue.offer(curr.left);if(curr.right!=null) queue.offer(curr.right);}res.addFirst(list);}return res;}
}







199. 二叉树的右视图

题目

给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

答案

class Solution {public List<Integer> rightSideView(TreeNode root) {List<Integer> res = new ArrayList();if(root==null){return res;}Queue<TreeNode> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();for(int i=0;i<size;i++){TreeNode curr = queue.poll();if(i==size-1){//判断是不是每一层的最后一个元素res.add(curr.val);}if(curr.left!=null) queue.offer(curr.left);if(curr.right!=null) queue.offer(curr.right);}}return res;}
}







637. 二叉树的层平均值

题目

给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5 以内的答案可以被接受。

答案

class Solution {public List<Double> averageOfLevels(TreeNode root) {List<Double> res = new ArrayList();if(root==null){return res;}Queue<TreeNode> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();double sum = 0;for(int i=0;i<size;i++){TreeNode curr = queue.poll();sum += curr.val;//计算每一层的和if(curr.left!=null) queue.offer(curr.left);if(curr.right!=null) queue.offer(curr.right);}res.add(sum/size);}return res;}
}







429. N 叉树的层序遍历

题目

给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。

树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。

答案

class Solution {public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> res = new ArrayList();if(root==null){return res;}Queue<Node> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();List<Integer> list = new ArrayList();for(int i=0;i<size;i++){Node curr = queue.poll();list.add(curr.val);for(Node node : curr.children){//遍历所有孩子if(node!=null) queue.offer(node);}}res.add(list);}return res;}
}







515. 在每个树行中找最大值

题目

给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。

答案

class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> res = new ArrayList();if(root==null){return res;}Queue<TreeNode> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();int max = Integer.MIN_VALUE;for(int i=0;i<size;i++){TreeNode curr = queue.poll();max = Math.max(max,curr.val);//计算每一层最大值if(curr.left!=null) queue.offer(curr.left);if(curr.right!=null) queue.offer(curr.right);}res.add(max);}return res;}
}







116. 填充每个节点的下一个右侧节点指针

题目

给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:

struct Node {int val;Node *left;Node *right;Node *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

初始状态下,所有 next 指针都被设置为 NULL

答案

class Solution {public Node connect(Node root) {if(root==null){return root;}Queue<Node> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();Node pre = queue.poll();if(pre.left!=null) queue.offer(pre.left);if(pre.right!=null) queue.offer(pre.right);for(int i=1;i<size;i++){Node curr = queue.poll();pre.next = curr;//将前一个 next 指向当前 pre = curr;if(curr.left!=null) queue.offer(curr.left);if(curr.right!=null) queue.offer(curr.right);}}return root;}
}








117. 填充每个节点的下一个右侧节点指针 II

题目

给定一个二叉树:

struct Node {int val;Node *left;Node *right;Node *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

初始状态下,所有 next 指针都被设置为 NULL

答案

class Solution {public Node connect(Node root) {if(root==null){return root;}Queue<Node> queue = new LinkedList();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();Node pre = queue.poll();if(pre.left!=null) queue.offer(pre.left);if(pre.right!=null) queue.offer(pre.right);for(int i=1;i<size;i++){Node curr = queue.poll();pre.next = curr;//将前一个 next 指向当前 pre = curr;if(curr.left!=null) queue.offer(curr.left);if(curr.right!=null) queue.offer(curr.right);}}return root;}
}

这篇关于树的遍历算法题总结(第二十六天)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/917709

相关文章

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

Nginx Location映射规则总结归纳与最佳实践

《NginxLocation映射规则总结归纳与最佳实践》Nginx的location指令是配置请求路由的核心机制,其匹配规则直接影响请求的处理流程,下面给大家介绍NginxLocation映射规则... 目录一、Location匹配规则与优先级1. 匹配模式2. 优先级顺序3. 匹配示例二、Proxy_pa

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

MySQL基本查询示例总结

《MySQL基本查询示例总结》:本文主要介绍MySQL基本查询示例总结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Create插入替换Retrieve(读取)select(确定列)where条件(确定行)null查询order by语句li

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

Java遍历HashMap的6种常见方式

《Java遍历HashMap的6种常见方式》这篇文章主要给大家介绍了关于Java遍历HashMap的6种常见方式,方法包括使用keySet()、entrySet()、forEach()、迭代器以及分别... 目录1,使用 keySet() 遍历键,再通过键获取值2,使用 entrySet() 遍历键值对3,

Linux区分SSD和机械硬盘的方法总结

《Linux区分SSD和机械硬盘的方法总结》在Linux系统管理中,了解存储设备的类型和特性是至关重要的,不同的存储介质(如固态硬盘SSD和机械硬盘HDD)在性能、可靠性和适用场景上有着显著差异,本文... 目录一、lsblk 命令简介基本用法二、识别磁盘类型的关键参数:ROTA查询 ROTA 参数ROTA