操作给定的二叉树,将其变换为源二叉树的镜像。 数据范围:二叉树的节点数 , 二叉树每个节点的值 要求: 空间复杂度 。本题也有原地操作,即空间复杂度 的解法,时间复杂度 比如: 源二叉树 镜像二叉树
示例1
输入
{8,6,10,5,7,9,11}
输出
{8,10,6,11,9,7,5}
说明
如题面所示
示例2
输入
{}
输出
{}
加载中...
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}
{}
{}