5.根据前序和中序得到二叉树代码实现,根据中序和后序得到二叉树代码实现(JavaScript版)

Posted lanshanxiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5.根据前序和中序得到二叉树代码实现,根据中序和后序得到二叉树代码实现(JavaScript版)相关的知识,希望对你有一定的参考价值。

根据前序和中序,使用javascript编写代码得到二叉树

技术图片
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var qian = ["a", "b", "d", "e", "c", "f", "g"];
        var zhong = ["d", "b", "e", "a", "f", "c", "g"];

        function Node(value){
            this.value = value;
            this.left = null;
            this.right = null;
        }

        //通过前序和中序还原二叉树
        function resumeTree(qian, zhong){
            if(qian == null || zhong == null || qian.length == 0 || zhong.length == 0 || qian.length != zhong.length) return null;//qian和zhong数组为null或者长度为0,或者两者长度不相等,直接返回
            var root = new Node(qian[0]);//前序第一个节点是二叉树的根节点
            var index = zhong.indexOf(qian[0]);//找到中序里根节点所在的索引
            var zhongLeft = zhong.slice(0, index);//获取中序的左子树
            var zhongRight = zhong.slice(index + 1, zhong.length);//获取中序的右子树
            var qianLeft = qian.slice(1, index + 1);//获取前序的左子树
            var qianRight = qian.slice(index + 1, qian.length);//获取前序的右子树
            root.left = resumeTree(qianLeft, zhongLeft);//找到节点的左节点
            root.right = resumeTree(qianRight, zhongRight);//找到节点的右节点

            return root;//返回二叉树
        }

        var tree = resumeTree(qian, zhong);//获取二叉树
        console.log(tree.left);//打印左子树
        console.log(tree.right);//打印右子树
    </script>
</body>

</html>
前序和中序的到二叉树.html

 

根据中序和后续,使用JavaScript编写代码得到二叉树

技术图片
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        
        var hou = ["d", "e", "b", "f", "g", "c", "a"];
        var zhong = ["d", "b", "e", "a", "f", "c", "g"];

        function Node(value){
            this.value = value;
            this.left = null;
            this.right = null;
        }

        //通过后序和中序还原二叉树
        function resumeTree(hou, zhong){
            if(hou == null || zhong == null || hou.length == 0 || hou.length == 0 || hou.length != zhong.length) return null;//hou和zhong数组为null或者长度为0,或者两者长度不相等,直接返回
            var root = new Node(hou[hou.length - 1]);//后序最后一个节点是二叉树的根节点
            var index = zhong.indexOf(hou[hou.length - 1]);//找到中序里根节点所在的索引
            var zhongLeft = zhong.slice(0, index);//获取中序的左子树
            var zhongRight = zhong.slice(index + 1, zhong.length);//获取中序的右子树
            var houLeft = hou.slice(0, index);//获取后序的左子树
            var houRight = hou.slice(index, hou.length - 1);//获取后序的右子树
            root.left = resumeTree(houLeft, zhongLeft);//找到节点的左节点
            root.right = resumeTree(houRight, zhongRight);//找到节点的右节点

            return root;//返回二叉树
        }

        var tree = resumeTree(hou, zhong);//获取二叉树
        console.log(tree.left);//打印左子树
        console.log(tree.right);//打印右子树
    </script>
</body>

</html>
View Code

 

以上是关于5.根据前序和中序得到二叉树代码实现,根据中序和后序得到二叉树代码实现(JavaScript版)的主要内容,如果未能解决你的问题,请参考以下文章

面试题07. 根据前序和中序重建二叉树

面试题07. 根据前序和中序重建二叉树

数据结构中已知前序序列和中序序列,怎么得出后序序列,谢谢回答!

构造一棵二叉树,并分别输出其先序遍历、中序遍历和后序遍历的结果

根据前序遍历和中序遍历得出后序遍历

通过遍历序列构造二叉树(扩展二叉树的先序先序和中序后序和中序层序和中序)附可执行完整代码