Given inorder and postorder traversal of a tree, construct the binary tree. Note: |
The following solution uses very similar approach to solve Below is the code:
|
/ * Definition for binary tree * struct TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public:
}; |
Provide a solution using Java.
|
|
|
Parse postorder array from the tail to head. Each node from postorder array separates inorder into two segments: left subtree and right subtree. Recursively build tree by this method.
|
1)the last node in postorder is the tree root; 2)find the root node in inorder, then get the left and right subtree order(both postorder and inorder list); 3)if can't find the root node in inorder, then the input is error, can not build the tree;
|