zoukankan      html  css  js  c++  java
  • 剑指offer面试题6:重建二叉树

    1、题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。

    public class Solution 
    {
        public TreeNode reConstructBinaryTree(int [] pre,int [] in)
        {
            TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
            return root;
        }
        //前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
        private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) 
        {     
            if(startPre>endPre||startIn>endIn)
                return null;
            TreeNode root=new TreeNode(pre[startPre]);
             
            for(int i=startIn;i<=endIn;i++)
                if(in[i]==pre[startPre])
                {
                    root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
                    root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
                }
                     
            return root;
        }
    }

    2、Java创建二叉树:

    public class TreeTest
    {
        public static String[] str;
        public static int count;
        /**
         * 静态内部类,定义二叉树节点
         */
        static class TreeNode
        {
            public String data;
            TreeNode lchild;
            TreeNode rchild;
            public TreeNode(String x)
            {
                this.data = x;
            }
        }
        /**
         * 根据前序序列递归构建二叉树
         * 
         * @return
         */
        public static TreeNode createBtree()
        {
            TreeNode root = null;
            if (count >= str.length || str[count++].equals("#"))
            {
                root = null;
            } 
            else
            {
                root = new TreeNode(str[count - 1]);
                root.lchild = createBtree();
                root.rchild = createBtree();
            }
            return root;
        }
        /**
         * 前序遍历
         * 
         * @param root
         */
        public static void preTraverse(TreeNode root)
        {
            if (root != null)
            {
                System.out.print(root.data + " ");
                preTraverse(root.lchild);
                preTraverse(root.rchild);
            }
        }
        /**
         * 中序遍历
         * 
         * @param root
         */
        public static void inTraverse(TreeNode root)
        {
            if (root != null)
            {
                inTraverse(root.lchild);
                System.out.print(root.data + " ");
                inTraverse(root.rchild);
            }
        }
        /**
         * 后序遍历
         * 
         * @param root
         */
        public static void postTraverse(TreeNode root)
        {
            if (root != null)
            {
                postTraverse(root.lchild);
                postTraverse(root.rchild);
                System.out.print(root.data + " ");
            }
        }
    
        public static void main(String args[])
        {
            Scanner cin = new Scanner(System.in);
            while (cin.hasNext())
            {
                String s = cin.nextLine();
                str = s.split(",");
                count = 0;
                TreeNode root = createBtree();
                // 前序遍历
                preTraverse(root);
                System.out.println();
                // 中序遍历
                inTraverse(root);
                System.out.println();
                // 后序遍历
                postTraverse(root);
                System.out.println();
            }
            cin.close();
        }
    }
  • 相关阅读:
    easyui datagrid 遇到的坑 cannot read property ·· pageNum bug and so on
    原生js上传图片时的预览
    yield的理解
    js时间格式化
    js 实现复制粘贴时注意方法中需要两次点击实现的bug
    jquery 页面分页的实现
    easyui dialog 表单提交,弹框初始化赋值,dialog实现
    a标签(普通标签如span)没有disabled属性 ,怎样利用js实现该属性
    js之数据类型(对象类型——构造器对象——数组2)
    js之数据类型(对象类型——构造器对象——数组1)
  • 原文地址:https://www.cnblogs.com/xujian2014/p/5315901.html
Copyright © 2011-2022 走看看