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

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

相关文章

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

java常见报错及解决方案总结

《java常见报错及解决方案总结》:本文主要介绍Java编程中常见错误类型及示例,包括语法错误、空指针异常、数组下标越界、类型转换异常、文件未找到异常、除以零异常、非法线程操作异常、方法未定义异常... 目录1. 语法错误 (Syntax Errors)示例 1:解决方案:2. 空指针异常 (NullPoi

Java反转字符串的五种方法总结

《Java反转字符串的五种方法总结》:本文主要介绍五种在Java中反转字符串的方法,包括使用StringBuilder的reverse()方法、字符数组、自定义StringBuilder方法、直接... 目录前言方法一:使用StringBuilder的reverse()方法方法二:使用字符数组方法三:使用自

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

Python依赖库的几种离线安装方法总结

《Python依赖库的几种离线安装方法总结》:本文主要介绍如何在Python中使用pip工具进行依赖库的安装和管理,包括如何导出和导入依赖包列表、如何下载和安装单个或多个库包及其依赖,以及如何指定... 目录前言一、如何copy一个python环境二、如何下载一个包及其依赖并安装三、如何导出requirem

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo