加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

[PHP] 算法-镜像二叉树的PHP实现

发布时间:2020-05-25 03:10:13 所属栏目:PHP 来源:互联网
导读:操作给定的二叉树,将其变换为源二叉树的镜像。二叉树的镜像定义:源二叉树8/6 10/ /5 7 9 11镜像二叉树8/10 6/ /11 9 7 5思路:1.左子树赋给temp2.temp赋给右子树3.右子树赋给左子树4.递归mirror(root)temp=root-leftroot-left=root-rightroot-right=tempmir

<div class="cnblogs_code">

8
           /6   10
         /   /5  7 9 118
           /10   6
         /   /11 9 7  51.2.3.4.=root->->left=root->-right=->->

<div class="cnblogs_code">

   =   =  __construct(->val =  Mirror(&(== =->->left=->->right=->-><span style="color: #008000">//<span style="color: #008000">构造一个树
<span style="color: #800080">$node5=<span style="color: #0000ff">new TreeNode(5<span style="color: #000000">);
<span style="color: #800080">$node7=<span style="color: #0000ff">new TreeNode(7<span style="color: #000000">);
<span style="color: #800080">$node9=<span style="color: #0000ff">new TreeNode(9<span style="color: #000000">);
<span style="color: #800080">$node11=<span style="color: #0000ff">new TreeNode(11<span style="color: #000000">);
<span style="color: #800080">$node6=<span style="color: #0000ff">new TreeNode(6<span style="color: #000000">);
<span style="color: #800080">$node10=<span style="color: #0000ff">new TreeNode(10<span style="color: #000000">);
<span style="color: #800080">$node8=<span style="color: #0000ff">new TreeNode(8<span style="color: #000000">);

<span style="color: #800080">$node8->left=<span style="color: #800080">$node6<span style="color: #000000">;
<span style="color: #800080">$node8->right=<span style="color: #800080">$node10<span style="color: #000000">;
<span style="color: #800080">$node6->left=<span style="color: #800080">$node5<span style="color: #000000">;
<span style="color: #800080">$node6->right=<span style="color: #800080">$node7<span style="color: #000000">;
<span style="color: #800080">$node10->left=<span style="color: #800080">$node9<span style="color: #000000">;
<span style="color: #800080">$node10->right=<span style="color: #800080">$node11<span style="color: #000000">;

<span style="color: #800080">$tree=<span style="color: #800080">$node8<span style="color: #000000">;

<span style="color: #008000">//<span style="color: #008000">镜像这棵二叉树
<span style="color: #008080">var_dump(<span style="color: #800080">$tree<span style="color: #000000">);
Mirror(<span style="color: #800080">$tree<span style="color: #000000">);
<span style="color: #008080">var_dump(<span style="color: #800080">$tree);

<div class="cnblogs_code">

(TreeNode)
  ["val"]=>8"left"]=>
  (TreeNode)
    ["val"]=>6"left"]=>
    (TreeNode)
      ["val"]=>5"left"]=>
      "right"]=>
      "right"]=>
    (TreeNode)
      ["val"]=>7"left"]=>
      "right"]=>
      "right"]=>
  (TreeNode)
    ["val"]=>10"left"]=>
    (TreeNode)
      ["val"]=>9"left"]=>
      "right"]=>
      "right"]=>
    (TreeNode)
      ["val"]=>11"left"]=>
      "right"]=>
      <span style="color: #0000ff">object(TreeNode)<span style="color: #008000">#<span style="color: #008000">7 (3) {
["val"]=><span style="color: #000000">
int(8<span style="color: #000000">)
["left"]=>
<span style="color: #0000ff">object(TreeNode)<span style="color: #008000">#<span style="color: #008000">6 (3) {
["val"]=><span style="color: #000000">
int(10<span style="color: #000000">)
["left"]=>
<span style="color: #0000ff">object(TreeNode)<span style="color: #008000">#<span style="color: #008000">4 (3) {
["val"]=><span style="color: #000000">
int(11<span style="color: #000000">)
["left"]=>
<span style="color: #0000ff">NULL<span style="color: #000000">
["right"]=>
<span style="color: #0000ff">NULL<span style="color: #000000">
}
["right"]=>
<span style="color: #0000ff">object(TreeNode)<span style="color: #008000">#<span style="color: #008000">3 (3) {
["val"]=><span style="color: #000000">
int(9<span style="color: #000000">)
["left"]=>
<span style="color: #0000ff">NULL<span style="color: #000000">
["right"]=>
<span style="color: #0000ff">NULL<span style="color: #000000">
}
}
["right"]=>
<span style="color: #0000ff">object(TreeNode)<span style="color: #008000">#<span style="color: #008000">5 (3) {
["val"]=><span style="color: #000000">
int(6<span style="color: #000000">)
["left"]=>
<span style="color: #0000ff">object(TreeNode)<span style="color: #008000">#<span style="color: #008000">2 (3) {
["val"]=><span style="color: #000000">
int(7<span style="color: #000000">)
["left"]=>
<span style="color: #0000ff">NULL<span style="color: #000000">
["right"]=>
<span style="color: #0000ff">NULL<span style="color: #000000">
}
["right"]=>
<span style="color: #0000ff">object(TreeNode)<span style="color: #008000">#<span style="color: #008000">1 (3) {
["val"]=><span style="color: #000000">
int(5<span style="color: #000000">)
["left"]=>
<span style="color: #0000ff">NULL<span style="color: #000000">
["right"]=>
<span style="color: #0000ff">NULL<span style="color: #000000">
}
}
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读