zoukankan      html  css  js  c++  java
  • 4、重建二叉树

    输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

    =============Python=============

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
            if len(inorder) == 0:
                return
            value = preorder.pop(0)
            ind = inorder.index(value)
            cur = TreeNode(value)
            cur.left = self.buildTree(preorder, inorder[:ind])
            cur.right = self.buildTree(preorder, inorder[ind+1:])
            return cur
    # -*- coding:utf-8 -*-
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    class Solution:
        # 返回构造的TreeNode根节点
        def reConstructBinaryTree(self, pre, tin):
            # write code here
            if not tin:
                return
            tmp = pre.pop(0)
            root = TreeNode(tmp)
            root.left = self.reConstructBinaryTree(pre, tin[0:tin.index(tmp)])
            root.right = self.reConstructBinaryTree(pre, tin[tin.index(tmp)+1:])
            return root

    ===============Java=================

    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
            if (pre == null || pre.length == 0 || in == null || in.length == 0 || pre.length != in.length) {
                return null;
            }
            return reConstruct(pre, in, 0, pre.length - 1, 0, in.length - 1);
        }
        
        public TreeNode reConstruct(int[] pre, int[] in, int preStart, int preEnd, int inStart, int inEnd) {
            if (preStart > preEnd || inStart > inEnd) {
                return null;
            }
            TreeNode head = new TreeNode(pre[preStart]);
            int inPos = 0;
            while (in[inPos] != pre[preStart]) {
                inPos++;
            }
            int minus = inPos - inStart;
            head.left = reConstruct(pre, in, preStart + 1, preStart + minus, inStart, inPos - 1);
            head.right = reConstruct(pre, in, preStart + minus + 1, preEnd, inPos + 1, inEnd);
            return head;
        }
    }
  • 相关阅读:
    关于vue2.x使用axios以及http-proxy-middleware代理处理跨域的问题
    vue-resource的使用
    从头开始开发一个vue幻灯片组件
    图与例解读Async/Await
    浅谈web缓存
    APICloud框架——总结一下最近开发APP遇到的一些问题 (三)
    编写现代 CSS 代码的 20 个建议
    仿微信联系人列表滑动字母索引
    初来乍到,向各位大牛虚心学习
    转发80端口的脚本
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13537899.html
Copyright © 2011-2022 走看看