Jerry's Notes

Contrust Binary Tree from Various Traversals

Word count: 771Reading time: 4 min
2022/01/14

Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 105. https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

Hint

preorder -> [root] [left subtree] [right subtree]

inorder -> [left subtree] [root] [right subtree]

  1. In Preorder Traversal, the first element is the root.
  2. In Inorder Traversal, the root divide the array into 2 parts: the front part is left children, and the latter part is right children.
  3. The length of the left children is i - inLeft.
  4. Use recursion.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {

public:
TreeNode* buildTree (vector<int>& preorder, vector<int>& inorder) {
int n = preorder.size();
return build(0, n - 1, 0, n - 1, preorder, inorder);
}
TreeNode* build (int preLeft, int preRight, int inLeft, int inRight, vector<int>& preorder, vector<int>& inorder) {
if (preLeft > preRight || inLeft > inRight) return nullptr;
TreeNode* root = new TreeNode(preorder[preLeft]);
int i = 0;
for (i = inLeft; i < inRight; i++) {
if (preorder[preLeft] == inorder[i]) break;
}
root->left = build(preLeft + 1, i - inLeft + preLeft, inLeft, i - 1, preorder, inorder);
root->right = build(i - inLeft + preLeft + 1, preRight, i + 1, inRight, preorder, inorder);
return root;
}
};

Construct Binary Tree from Inorder and Postorder Traversal

LeetCode 106. https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/

Hint

inorder -> [left subtree] [root] [right subtree]

postorder -> [left subtree] [right substree] [root]

Almost same with the LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal. The Last element of postorder traversal is the root node.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* buildTree (vector<int>& inorder, vector<int>& postorder) {
int n = inorder.size();
return build(0, n - 1, 0, n - 1, inorder, postorder);
}

TreeNode* build (int postLeft, int postRight, int inLeft, int inRight, vector<int>& inorder, vector<int>& postorder) {
if (postLeft > postRight || inLeft > inRight) return NULL;
TreeNode* root = new TreeNode(postorder[postRight]);
int i = 0;
for (i = inLeft; i < inRight; i++) {
if (postorder[postRight] == inorder[i]) break;
}
root->left = build(postLeft, i - inLeft + postLeft - 1, inLeft, i - 1, inorder, postorder);
root->right = build(i - inLeft + postLeft, postRight - 1, i + 1, inRight, inorder, postorder);
return root;
}
};

Construct Binary Tree from Preorder and Postorder Traversal

LeetCode 889. https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/

Hint

preorder -> [root] [left subtree] [right subtree]

postorder -> [left subtree] [right substree] [root]

exampel:

preorder -> [root] [2,4,5] [3,6,7]

postorder -> [4,5,2] [6,7,3] [root]

  1. In Preorder Traversal, preorder[1] is the first node of left children. In Postorder Traversal, this element will appear in the last position of left children range.
  2. The length of left children is i - postLeft + 1.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* constructFromPrePost (vector<int>& preorder, vector<int>& postorder) {
int n = preorder.size();
return build(0, n - 1, 0, n - 1, preorder, postorder);
}
TreeNode* build (int preLeft, int preRight, int postLeft, int postRight, vector<int>& preorder, vector<int>& postorder) {
if (preLeft > preRight || postLeft > postRight) return NULL;
TreeNode* root = new TreeNode(preorder[preLeft]);
if(preLeft == preRight) return root; // corner case: only have the root node
int i = 0;
for (i = postLeft; i < postRight; i++) {
// find the first left child
if (preorder[preLeft + 1] == postorder[i]) break;
}
int leftLen = i - postLeft + 1;
root->left = build(preLeft + 1, preLeft + leftLen, postLeft, i, preorder, postorder);
root->right = build(preLeft + leftLen + 1, preRight, i + 1, postRight - 1, preorder, postorder);
return root;
}
};