• 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;
        }
    }
  • 相关阅读:
    jQuery的父,子,兄弟节点查找方法
    我们的产品最终入驻淘宝服务市场
    iframe显示滚动栏
    自己定义控件三部曲之动画篇(十三)——实现ListView Item进入动画
    [叁]Pomelo游戏server编程分享 之 server结构与配置分析
    数列求和
    字节与字符_字节流与字符流_ASCII与Unicode_GB2312_GBK_GB18030_BIG-5
    Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)
    呼叫保持流程
    PDF转EPUB格式电子书经验总结
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13537899.html
Copyright © 2020-2023  润新知