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

[PHP] 算法-根据前序和中序遍历结果重建二叉树的PHP实现

发布时间:2020-05-25 03:10:31 所属栏目:PHP 来源:互联网
导读:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。1.前序遍历是中,左,右;中序遍历是左,

<div class="cnblogs_code">

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,1,8,61.<span style="color: #000000">前序遍历是中,左,右;中序遍历是左,中,右
2.<span style="color: #000000">前序遍历的第一个是根结点,中序遍历数组中从开始到根结点的所有是左子树,可以知道左子树的个数,根结点右边的是右子树
3.<span style="color: #000000">前序遍历除去0位置的,从1到左子树个数位置是左子树,其他的是右子树
4.<span style="color: #000000">确定四个数组,前序左子树数组,前序右子树数组,中序左子树数组,中序右子树数组;递归调用

reConstructBinaryTree(pre,<span style="color: #000000">in)
<span style="color: #0000ff">if(pre.length) <span style="color: #0000ff">return <span style="color: #0000ff">null<span style="color: #008000">//<span style="color: #008000">递归终止条件
root=pre[0<span style="color: #000000">]
Node=<span style="color: #0000ff">new<span style="color: #000000"> Node(root)
<span style="color: #008000">//<span style="color: #008000">在中序中找根结点的位置
p=0
<span style="color: #0000ff">for p;p<pre.length;p++
<span style="color: #0000ff">if in[p]==root <span style="color: #0000ff">break
<span style="color: #0000ff">for i=0;i<pre.length;i++

    <span style="color: #0000ff"&gt;if</span> i<<span style="color: #000000"&gt;p
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;中序左子树数组</span>
        inLeft[]=<span style="color: #000000"&gt;in[i]
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;前序左子树数组</span>
        preLeft[]=pre[i+1<span style="color: #000000"&gt;]
    </span><span style="color: #0000ff"&gt;else</span> <span style="color: #0000ff"&gt;if</span> i><span style="color: #000000"&gt;p
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;中序的右子树</span>
        inRight[]=<span style="color: #000000"&gt;in[i]
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;前序的右子树</span>
        preRight[]=<span style="color: #000000"&gt;pre[i]
Node</span>->left=reConstructBinaryTree(preLeft,<span style="color: #000000"&gt;inLeft)
Node</span>->right=reConstructBinaryTree(preRight,<span style="color: #000000"&gt;inRight)
</span><span style="color: #0000ff"&gt;return</span> Node</pre>
val = reConstructBinaryTree(,=((==0 =[0= TreeNode((=0;<;++([]======(=0;<;++(<[]=[+1[]=[ (>[]=[[]=[->left=reConstructBinaryTree(,->right=reConstructBinaryTree(, <span style="color: #800080">$pre=<span style="color: #0000ff">array(1,8<span style="color: #000000">);
<span style="color: #800080">$vin=<span style="color: #0000ff">array(4,6<span style="color: #000000">);
<span style="color: #800080">$node=reConstructBinaryTree(<span style="color: #800080">$pre,<span style="color: #800080">$vin<span style="color: #000000">);;
<span style="color: #008080">var_dump(<span style="color: #800080">$node);

(TreeNode) ["val"]=>1"left"]=> (TreeNode) ["val"]=>2"left"]=> (TreeNode) ["val"]=>4"left"]=> "right"]=> (TreeNode) ["val"]=>7"left"]=> "right"]=> "right"]=> "right"]=> (TreeNode) ["val"]=>3"left"]=> (TreeNode) ["val"]=>5"left"]=> "right"]=> "right"]=> (TreeNode) ["val"]=>6"left"]=> (TreeNode) ["val"]=>8"left"]=> "right"]=> "right"]=>

(编辑:安卓应用网)

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

    推荐文章
      热点阅读