题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
<?php
/*class TreeNode{
var $val;
var $left = NULL;
var $right = NULL;
function __construct($val){
$this->val = $val;
}
}*/
function Mirror(&$root)
{
// write code here
$tree=new TreeNode(null);
if($root!=null){
$tree=$root->right;
$root->right=$root->left;
$root->left=$tree;
if($root->left!=null){
Mirror($root->left);
}
if($root->right!=null){
Mirror($root->right);
}
}
return $root;
}
本文介绍了一种将给定二叉树转换为其镜像的方法。通过递归地交换二叉树节点的左子树和右子树来实现。文章提供了一个PHP实现的例子。
 第十八题 二叉树的镜像&spm=1001.2101.3001.5002&articleId=80000617&d=1&t=3&u=96d3d3fe864145f7bbbf67ec8758344b)
1万+

被折叠的 条评论
为什么被折叠?



