首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
实现二叉树先序,中序和后序遍历
[编程题]实现二叉树先序,中序和后序遍历
热度指数:175681
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一棵二叉树,分别按照二叉树先序,中序和后序打印所有的节点。
数据范围:
,树上每个节点的val值满足
要求:空间复杂度
,时间复杂度
样例解释:
如图二叉树结构
示例1
输入
{1,2,3}
输出
[[1,2,3],[2,1,3],[2,3,1]]
说明
如题面图
示例2
输入
{}
输出
[[],[],[]]
备注:
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(13)
邀请回答
收藏(816)
分享
提交结果有问题?
308个回答
276篇题解
开通博客
牛一霸
发表于 2021-07-13 22:14:29
精华题解
题目:实现二叉树先序,中序,后序的遍历 描述:分别按照二叉树先序,中序和后序打印所有的节点。 示例1:输入:{1,2,3},返回值:[[1,2,3],[2,1,3],[2,3,1]] 解法一: 思路分析:既然题目中提到二叉树先序,中序,后序遍历,那么我们就先聊一聊
展开全文
牛客786963925号
发表于 2021-07-14 00:01:56
精华题解
解法一:递归 「二叉树的先序遍历」的思路是:先访问根结点,再访问左子树,最后访问右子树; 「二叉树的中序遍历」的思路是:先访问左子树,再访问根结点,最后访问右子树; 「二叉树的后序遍历」的思路是:先访问左子树,再访问右子树,最后访问根结点; 下图以「先序遍历」为例进行展示: 基于上述思路,可以得
展开全文
堆栈哲学
发表于 2021-07-12 23:28:59
精华题解
题意分析: 数据结构基础知识,考察二叉树的三序遍历。 二叉树的三序遍历时基础,不了解或者已经忘记的玩家可以看一下图解二叉树的三序遍历 图解: 解法一:递归 前序遍历: 访问顺序:根节点——>左子树——>右子树的方式遍历这棵树 而在访问左子树或者右子树的时候,我们按照同样的方式
展开全文
未来0116
发表于 2021-07-12 12:22:04
精华题解
一.题目描述NC45实现二叉树先序,中序和后序遍历分别按照二叉树先序遍历、中序遍历和后序遍历打印所有的节点。二.算法(递归实现)以先序遍历来解释,首先我们需要了解什么是二叉树的先序遍历:按照访问根节点-左子树-右子树的方式遍历这棵树,而在访问左子树和右子树的时候我们按照同样的方式遍历,直到遍历整棵树
展开全文
蒙牛麦片
发表于 2021-07-16 22:25:49
精华题解
NC45 实现二叉树先序,中序和后序遍历 题意分析: 实现二叉树的先序,中序,后续遍历 题解一(递归遍历): 比如有二叉树 先序遍历:按照访问树根,左子树,右子树的顺序对树进行遍历,子树同样遵守这个遍历顺序 如图所示的二叉树:先序遍历结果为4,2,1,3,5. 代码实现如下: void preO
展开全文
麻豆出品
发表于 2020-09-08 01:36:39
最近东京有点热,撸道二叉树遍历解解暑气。思路:二叉树遍历没啥难度,按照常规操作递归遍历即可。先序:根左右中序:左根右后序:左右根 代码: class TreeNode: def __init__(self, x): self.val = x self.le
展开全文
下一次什么时候可以修改昵称
发表于 2020-11-01 14:06:47
递归 算法 递归 public void preorder(TreeNode root, List<Integer> list) { if (root != null) { list.add(root.val); preorder(root
展开全文
henryboy233
发表于 2021-12-09 12:26:14
# def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 #
展开全文
NTJD
发表于 2020-11-09 14:20:21
实现二叉树先序中序后序遍历 牛客题霸NC45 难度:Easy 题目描述 分别按照二叉树先序,中序和后序打印所有的节点。 示例 输入 {1,2,3}返回值 [[1,2,3],[2,1,3],[2,3,1]]备注: n≤10^6题目答案 import java.util.*; /* * publ
展开全文
java小和尚
发表于 2020-09-07 15:42:01
import java.util.*; /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; } / public class Solution { /** *
展开全文
sevenyang96
发表于 2020-09-08 21:37:07
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ vector<int> pre; vector<int>
展开全文
LaN666
发表于 2020-11-19 00:54:24
版本一:使用递归进行遍历、 import java.util.*; /* * public class TreeNode { * int val = 0; &
展开全文
ypqhappy
发表于 2021-04-07 22:42:21
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** *
展开全文
shredderzwj
发表于 2021-11-30 14:51:42
递归 # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定
展开全文
大力男友
发表于 2022-01-12 11:41:57
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 the root of
展开全文
问题信息
栈
树
哈希
难度:
308条回答
816收藏
17591浏览
热门推荐
通过挑战的用户
查看代码
牛客20133...
2023-03-14 14:38:13
zhancha...
2023-03-10 20:21:18
K乀
2023-03-04 16:37:51
已注销
2023-02-17 16:41:19
普信员
2023-01-07 22:03:13
相关试题
执行以下程序,理论上输出的结果应最...
360集团
Python
算法工程师
2019
评论
(1)
来自
360公司-2019校招...
下面关于 Spring Cloud...
Spring
评论
(1)
以下描述正确的是
Java
评论
(1)
测试设计员的职责: ①制定测试计划...
软件测试
评论
(1)
下列哪个选项可以用于在Java中将...
Java
评论
(1)
实现二叉树先序,中序和后序遍历
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ public int[][] threeOrders (TreeNode root) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型vector
> */ vector
> threeOrders(TreeNode* root) { // write code here } };
#coding:utf-8 # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 the root of binary tree # @return int整型二维数组 # class Solution: def threeOrders(self , root ): # write code here
using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ public List
> threeOrders (TreeNode root) { // write code here } }
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ function threeOrders( root ) { // write code here } module.exports = { threeOrders : threeOrders };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ function threeOrders( $root ) { // write code here }
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 the root of binary tree # @return int整型二维数组 # class Solution: def threeOrders(self , root: TreeNode) -> List[List[int]]: # write code here
package main import "fmt" import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ func threeOrders( root *TreeNode ) [][]int { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 * @return int* returnSize 返回数组行数 * @return int** returnColumnSizes 返回数组列数 */ int** threeOrders(struct TreeNode* root, int* returnSize, int** returnColumnSizes ) { // write code here }
# class TreeNode # attr_accessor :val, :left, :right # def initialize(val, left = nil, right = nil) # @val, @left, @right = val, left, right # end # end # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 the root of binary tree # @return int整型二维数组 # class Solution def threeOrders(root) # write code here end end
/** * class TreeNode(var val: Int) { * var left: TreeNode = null * var right: TreeNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ def threeOrders(root: TreeNode): Array[Array[Int]] = { // write code here } }
/** * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ fun threeOrders(root: TreeNode?): Array
{ // write code here } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ public int[][] threeOrders (TreeNode root) { // write code here } }
/*class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ export function threeOrders(root: TreeNode): number[][] { // write code here }
/** * public class TreeNode { * public var val: Int * public var left: TreeNode? * public var right: TreeNode? * public init(_ val: Int=0, _ left: TreeNode?=nil, _ right: TreeNode?=nil) { * self.val = val * self.left = left * self.right = right * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ func threeOrders ( _ root: TreeNode?) -> [[Int]] { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option
>, * pub right: Option
>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ pub fn threeOrders(&self, root: Option
>) -> Vec
> { // write code here } }
{1,2,3}
[[1,2,3],[2,1,3],[2,3,1]]
{}
[[],[],[]]