首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
二叉树的镜像
[编程题]二叉树的镜像
热度指数:133762
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
操
作给定的二叉树,将其变换为源二叉树的镜像。
数据范围:二叉树的节点数
, 二叉树每个节点的值
要求: 空间复杂度
。本题也有原地操作,即空间复杂度
的解法,时间复杂度
比如:
源二叉树
镜像二叉树
示例1
输入
{8,6,10,5,7,9,11}
输出
{8,10,6,11,9,7,5}
说明
如题面所示
示例2
输入
{}
输出
{}
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(792)
分享
提交结果有问题?
249个回答
313篇题解
开通博客
Maokt
发表于 2021-06-25 17:30:12
精华题解
算法思想一:递归 解题思路: 根据二叉树镜像的定义,考虑递归遍历(dfs)二叉树,交换每个节点的左 / 右子节点,即可生成二叉树的镜像。 解题步骤: 1、特判:如果pRoot为空,返回空 2、交换左右子树 3、把pRoot的左子树放到Mirror中镜像一下 4、把pRo
展开全文
开车的阿Q
发表于 2021-07-18 17:48:51
精华题解
描述 这是一篇面对初级coder的题解。 知识点:树 递归 DFS 难度:一星 题解 题目:操作给定的二叉树,将其变换为源二叉树的镜像。 比如: 源二叉树 8
展开全文
牛客题解官
发表于 2022-04-22 12:01:07
精华题解
题目的主要信息: 将二叉树镜像,即将其所有左右子树交换 我们可以考虑自底向上依次交换二叉树的左右节点。 举一反三: 学习完本题的思路你可以解决如下题目: BM28. 二叉树的最大深度 BM29. 二叉树中和为某一值的路径(一) BM31. 对称的二叉树 BM32. 合并二叉树 BM36. 判断是
展开全文
Maokt
发表于 2021-07-19 11:22:53
精华题解
算法思想一:递归 解题思路: 根据二叉树镜像的定义,考虑递归遍历二叉树,交换每个节点的左 / 右子节点,即可生成二叉树的镜像 算法流程: 1、终止条件: 当节点 pRoot 为空时(即越过叶节点),则返回 None;
展开全文
枫叶零渡
发表于 2021-06-30 21:20:27
精华题解
题目描述 引用内容操作给定的二叉树,将其变换为源二叉树的镜像。比如: 源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8
展开全文
大菠萝侦探
发表于 2021-07-01 01:29:33
精华题解
描述 操作给定的二叉树,将其变换为原二叉树的镜像。 算法思路 这道题采用的是自顶向下的递归蓝色子树是已经完成交换的子树,绿色子树是即将进行交换的左子树,绿色子树右边的橙色子树是将要和绿色子树交换的子树 对于当前的 root 交换左右子树 交换之后如下 对于每个子树的左右子树重复上面第一步的操作
展开全文
2019113913
发表于 2021-07-17 22:47:18
精华题解
题意思路:操作给定的二叉树,将其变换为源二叉树的镜像。也就是将二叉树的每个节点的左子树换成右子树,右子树换成左子树 方法一:递归处理先处理当前root的左右节点,使当前节点的左子树与右子树交换,达到镜像目的, 然后再递归处理左子树和右子树即可。 特例当pRoot为空时,直接返回pRoot。复杂度分析
展开全文
数据结构和算法
发表于 2021-03-21 22:15:05
1,BFS解决 之前讲373,数据结构-6,树的时候,提到过二叉树的广度优先搜索,就是一层一层的访问,像下面这样二叉树的BFS代码如下 public static void treeBFS(TreeNode root) { //如果为空直接返回 if (root == null)
展开全文
咪咪虾条001
发表于 2021-02-26 19:31:10
使用递归思想:先镜像当前root左右节点,然后再递归镜像左孩子和右孩子即可。代码如下: public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param p
展开全文
不经历怎么能成长
发表于 2021-03-04 16:09:28
/* f():将树镜像翻转返回指针 递归出口:当pRoot==NULL return NULL;return pRoot; 左右子树翻转f(pRoot->left);f(pRoot->right) tmp=pRoot->right;保存一个子树的指针交换时防止被覆盖 pRoot-&
展开全文
蓝色河马
发表于 2022-03-18 10:33:39
首先想到的就是递归,递归总是从解决一个大问题开始,不断转化为小的子问题,最后通过最小子问题的答案再推出大问题的最终结果。 本题中的大问题是输入一个根节点,输出树的镜像,可以分两步: 交换左右子树 得到左右子树的镜像 终止条件为左右子树为空 /* * function TreeNode(x) {
展开全文
不是江小白
发表于 2021-08-11 00:12:26
1. 解题思路 仔细看题目描述里面的两颗二叉树,可以发现镜像二叉树的左右子树就是交换源二叉树的左右子树镜像后得到的!所以只需先遍历求出源二叉树的left 和right 子树,然后再获取他们的镜像子树,最后交换镜像子树的位置即可得到。 2. 核心代码 # class TreeNode: # d
展开全文
肥肥澡
发表于 2021-03-07 19:15:31
case 1如果结点为NULL或者左右子树都是NULL,直接返回结点指针case 2如果结点有左右指针,交换左右指针,然后镜像左子树,镜像右子树,返回结点指针 class Solution { public: TreeNode* Mirror(TreeNode* pRoot) {
展开全文
wkkw
发表于 2021-10-08 23:15:44
题目很直接,很简单啊 TreeNode* Mirror(TreeNode* pRoot) { // write&
展开全文
Hzu_Lai
发表于 2022-02-18 15:54:56
dfs递归遍历二叉树,从底向上依次交换各个节点的左右子节点,生成镜像二叉树 public class Solution { public TreeNode Mirror (TreeNode pRoot) { // write code here if(pRoot==null || (
展开全文
你敲代码的样子好像蔡徐坤
发表于 2021-09-18 10:14:03
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), righ
展开全文
牛客308632849号
发表于 2022-03-21 21:52:05
用递归把左右子树交换位置。 struct TreeNode* Mirror(struct TreeNode* pRoot ) { // write code here struct TreeNode* tmp; if(!pRoot) return NULL; tmp
展开全文
问题信息
树
上传者:
牛客301499号
难度:
249条回答
792收藏
6196浏览
热门推荐
通过挑战的用户
查看代码
李逢溪
2023-03-13 10:59:20
皇家化院
2023-02-26 14:36:20
随机昵称都有什...
2023-02-21 17:20:26
讲义气的山羊希...
2023-02-16 11:18:43
侬是一个好人
2023-02-03 14:58:05
相关试题
设某二叉树的先序遍历序列为abdg...
树
评论
(1)
游戏内数据分析涉猎的少,如何证明自...
评论
(1)
之前的经历中单品数据分析的经验丰富...
评论
(1)
什么样的人适合做数据分析
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(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 pRoot TreeNode类 * @return TreeNode类 */ public TreeNode Mirror (TreeNode pRoot) { // 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 pRoot TreeNode类 * @return TreeNode类 */ TreeNode* Mirror(TreeNode* pRoot) { // write code here } };
#coding:utf-8 # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return TreeNode类 # class Solution: def Mirror(self , pRoot ): # 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 pRoot TreeNode类 * @return TreeNode类 */ public TreeNode Mirror (TreeNode pRoot) { // write code here } }
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return TreeNode类 */ function Mirror( pRoot ) { // write code here } module.exports = { Mirror : Mirror };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return TreeNode类 */ function Mirror( $pRoot ) { // write code here }
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return TreeNode类 # class Solution: def Mirror(self , pRoot: TreeNode) -> TreeNode: # write code here
package main import "fmt" import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return TreeNode类 */ func Mirror( pRoot *TreeNode ) *TreeNode { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return TreeNode类 */ struct TreeNode* Mirror(struct TreeNode* pRoot ) { // 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 pRoot TreeNode类 # @return TreeNode类 # class Solution def Mirror(pRoot) # write code here end end
/** * class TreeNode(var val: Int) { * var left: TreeNode = null * var right: TreeNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return TreeNode类 */ def Mirror(pRoot: TreeNode): TreeNode = { // write code here } }
/** * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return TreeNode类 */ fun Mirror(pRoot: TreeNode?): TreeNode? { // 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 pRoot TreeNode类 * @return TreeNode类 */ public TreeNode Mirror (TreeNode pRoot) { // 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 pRoot TreeNode类 * @return TreeNode类 */ export function Mirror(pRoot: TreeNode): TreeNode { // 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 pRoot TreeNode类 * @return TreeNode类 */ func Mirror ( _ pRoot: TreeNode?) -> TreeNode? { // 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 pRoot TreeNode类 * @return TreeNode类 */ pub fn Mirror(&self, pRoot: Option
>) -> Option
> { // write code here } }
{8,6,10,5,7,9,11}
{8,10,6,11,9,7,5}
{}
{}