1. 程式人生 > >LeetCode106 從中序和後序序列構造二叉樹

LeetCode106 從中序和後序序列構造二叉樹

leetcode1 tor def 中序遍歷 == ron tro 條件 data

題目描述:

根據一棵樹的中序遍歷與後序遍歷構造二叉樹。

註意:
你可以假設樹中沒有重復的元素。

例如,給出

中序遍歷 inorder = [9,3,15,20,7]
後序遍歷 postorder = [9,15,7,20,3]

返回如下的二叉樹:

    3
   /   9  20
    /     15   7





/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 
*/ /* 算法思想: 由於後序的順序的最後一個肯定是根,所以原二叉樹的根節點可以知道,題目中給了一個很關鍵的條件就是樹中沒有相同元素,有了這個條件我們就可以在中序遍歷中也定位出根節點的位置,並以根節點的位置將中序遍歷拆分為左右兩個部分,分別對其遞歸調用原函數。 需要小心的地方就是遞歸是postorder的左右index很容易寫錯,比如 pLeft + i - iLeft - 1, 這個又長又不好記,首先我們要記住 i - iLeft 是計算inorder中根節點位置和左邊起始點的距離,然後再加上postorder左邊起始點然後再減1。我們可以這樣分析,如果根節點就是左邊起始點的話,那麽拆分的話左邊序列應該為空集,此時i - iLeft 為0, pLeft + 0 - 1 < pLeft, 那麽再遞歸調用時就會返回NULL, 成立。如果根節點是左邊起始點緊跟的一個,那麽i - iLeft 為1, pLeft + 1 - 1 = pLeft,再遞歸調用時還會生成一個節點,就是pLeft位置上的節點,為原二叉樹的一個葉節點。
*/ //算法實現: class Solution { public: public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { return buildTree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1); } TreeNode *buildTree(vector<int> &inorder, int
iLeft, int iRight, vector<int> &postorder, int pLeft, int pRight) { if (iLeft > iRight || pLeft > pRight) return NULL; TreeNode *cur = new TreeNode(postorder[pRight]); int i = 0; for (i = iLeft; i < inorder.size(); ++i) { if (inorder[i] == cur->val) break; } cur->left = buildTree(inorder, iLeft, i - 1, postorder, pLeft, pLeft + i - iLeft - 1); cur->right = buildTree(inorder, i + 1, iRight, postorder, pLeft + i - iLeft, pRight - 1); return cur; } };

LeetCode106 從中序和後序序列構造二叉樹