力扣:面試題07&105題 : 根據二叉樹的前序中序遍歷重建二叉樹

題目

輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建該二叉樹。
假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。

例如,給出

前序遍歷 preorder = [3,9,20,15,7]
中序遍歷 inorder = [9,3,15,20,7]
返回如下的二叉樹:

    3
   / \
  9  20
    /  \
   15   7
限制:
0 <= 節點個數 <= 5000

解題思路

該題主要考察前序遍歷/中序遍歷的遍歷順序
前序遍歷: 由根節點開始訪問,有左子節點就訪問左子節點,最後訪問右子節點;前序遍歷中根節點在序列的首位
    例: [3,9,20,15,7]
中序遍歷: 由左子節點開始再訪問根節點再訪問右子節點;中序遍歷根節點在序列的中間,左邊是左子樹右邊是右子樹
    例:[9,3,15,20,7]
後序遍歷: 由左子節點開始,再訪問右子節點,最後訪問根節點;後序遍歷根節點在序列的末位
    例:9 15 7 20 3

題目是根據前序遍歷和中序遍歷進行二叉樹的重建,所以就要分析每個節點在前序和中序中的位置關係。
1.前序遍歷中,根節點總是在該樹的遍歷結果的第一個位置。所以3是總的根節點。
2.中序遍歷中,一個節點的左右子樹是在根節點的左右兩側的,所以中序遍歷中3的左邊是左子樹序列,右邊是右子樹序列
3.依照此順序依次遞歸各自子樹即可。

題解

/**
 * @ClassName LKMS07
 * @Description TODO 力扣劍指offer面試題 07
 * @Author HeXiaoyuan
 * @Date 2020-06-22 9:21
 */
public class LKMS07 {

    class Solution {
        /**
         * @Description: 先在前序遍歷序列中定位根節點值,然後在中序遍歷序列中定位左子樹和右子樹
         * @Author: HeXiaoyuan
         * @Date: 2020-06-22 9:23
         * @param preorder: 前序遍歷序列
         * @param inorder: 中序遍歷序列
         * @return: {@link TreeNode}
         **/
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            //判斷是否是空樹
            if(preorder==null||preorder.length==0){
                return null;
            }
            int length = preorder.length;
            //記錄中序中所有值的索引,省的遍歷查詢了
            Map<Integer , Integer> indexMap = new HashMap<>();
            for (int i = 0 ; i < length ; i++){
                indexMap.put(inorder[i],i);
            }
            TreeNode tree = buildTree(preorder,0,length-1,inorder,0,length-1,indexMap);
            return tree;
        }
        // 四個起始參數: 該次遞歸的前序序列的起始範圍;中序序列的起始範圍
        public TreeNode buildTree (int[] preorder , int preStart , int preEnd 
        							, int[] inorder ,int inStart , int inEnd
        							,Map<Integer,Integer> indexMap){
            //判斷是否已經遞歸到最後節點
            if(preStart > preEnd){
                return null;
            }
            //創建當前節點
            int rootVal = preorder[preStart];
            TreeNode rootNode = new TreeNode(rootVal);
            //起始位置都相同,說明該節點無子樹
            if(preStart == preEnd){
                return rootNode;
            }else{
                //找出中序遍歷中的根節點位置
                int rootOrderIndex = indexMap.get(rootVal);
                int leftNodeLength = rootOrderIndex - inStart;
				//計算該節點的左子樹: 
				//前序起始範圍爲:當前前序起始+1,前序起始+左子樹長度;
				//中序起始範圍爲:當前中序開始,當前中序根節點位置-1;
                TreeNode leftTree = buildTree(preorder,preStart+1,preStart+leftNodeLength
                					,inorder,inStart,rootOrderIndex-1,indexMap);
				//計算該節點的右子樹: 
				//前序起始範圍爲:當前前序起始+左子樹長度+1,當前前序結束;
				//中序起始範圍爲:當前中序根節點位置+1,當前中序結束;
                TreeNode rightTree = buildTree(preorder,preStart+leftNodeLength+1,preEnd
                					 ,inorder,rootOrderIndex+1,inEnd,indexMap);
                rootNode.left = leftTree;
                rootNode.right = rightTree;
            }

            return rootNode;
        }
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}

在這裏插入圖片描述

最快解答方案:非常快

/**
* 根據前序遍歷中序遍歷的特性;前序遍歷的節點首位一定是二叉樹的根節點,且前序會一根筋的只要有左節點就會一直將左節點羅列完;
* 中序遍歷的首位必定先左子節點;
* 所以只需要向前推進前序二叉樹,並且判斷該節點是否有左右子樹需要遞歸即可;
* 所以依照前序遍歷序列向前推進,噹噹前節點和中序的當前索引節點相同,說明已經到了葉子節點
* 再遞歸另一條樹即可,此時前序序列也轉入了右子樹序列段。
* 該題解主要是在找當前根節點的左右子節點的葉子節點(在前序中的邊界值),然後進入另一個子樹的遞歸中去
*/
class FastSolution {
        int preindex = 0;
        int inindex = 0;

        public TreeNode buildTree(int[] preorder, int[] inorder) {

            return buildtree(preorder,inorder,null);

        }

        public TreeNode buildtree(int[] preorder,int[] inorder, TreeNode finish){

            if(preindex == preorder.length ||(finish != null && inorder[inindex] == finish.val)){
                return null;
            }
            //第一次創建的時候就是以第一個根節點創建的
            TreeNode root = new TreeNode(preorder[preindex ++]);
            //左子樹
            root.left = buildtree(preorder, inorder ,root);
            inindex ++;
            //右子樹
            root.right = buildtree(preorder,inorder,finish);

            return root;


        }
    }

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章