[LeetCode-21]Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

There is an example.
        _______7______
       /              \
    __10__          ___2
   /      \        /
   4       3      _8
            \    /
             1  11
The preorder and inorder traversals for the binary tree above is:
preorder = {7,10,4,3,1,2,8,11}
inorder = {4,10,3,1,7,11,8,2}

The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder:  {7}, {10,4,3,1}, {2,8,11}
inorder:     {4,10,3,1}, {7}, {11, 8,2}

        _______7______
       /              \
    {4,10,3,1}       {11,8,2}
Since we alreay find that {7} will be the root, and in "inorder" sert, all the data in the left of {7} will construct the left sub-tree. And the right part will construct a right sub-tree. We can the left and right part agin based on the preorder.
2nd round
left part                                                                            right part
preorder: {10}, {4}, {3,1}                                              {2}, {8,11}
inorder:  {4}, {10}, {3,1}                                                {11,8}, {2}


        _______7______
       /              \
    __10__          ___2
   /      \        /
   4      {3,1}   {11,8}
see that, {10} will be the root of left-sub-tree and {2} will be the root of right-sub-tree.

Same way to split {3,1} and {11,8}, yo will get the complete tree now.

        _______7______
       /              \
    __10__          ___2
   /      \        /
   4       3      _8
            \    /
             1  11
So, simulate this process from bottom to top with recursion as following code.
c++

TreeNode *BuildTreePI(
    vector<int> &preorder,
    vector<int> &inorder,
    int p_s, int p_e,
    int i_s, int i_e){
    if(p_s > p_e) return NULL;
    int pivot = preorder[p_s];
    int i = i_s;
    for(;i<i_e;i++){
        if(inorder[i] == pivot)
            break;
    }
    int length1 = i-i_s-1;
    int length2 = i_e-i-1;
    TreeNode* node = new TreeNode(pivot);
    node->left = BuildTreePI(preorder,inorder,p_s+1,length1+p_s+1,i_s, i-1);
    node->right = BuildTreePI(preorder, inorder, p_e-length2, p_e, i+1, i_e);
    return node;
}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
    return BuildTreePI(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
}

java

public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildPI(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
    }
	public TreeNode buildPI(int[] preorder, int[] inorder, int p_s, int p_e, int i_s, int i_e){
		if(p_s>p_e)
			return null;
		int pivot = preorder[p_s];
		int i = i_s;
		for(;i<i_e;i++){
			if(inorder[i]==pivot)
				break;
		}
		TreeNode node = new TreeNode(pivot);
		int lenLeft = i-i_s;
		node.left = buildPI(preorder, inorder, p_s+1, p_s+lenLeft, i_s, i-1);
		node.right = buildPI(preorder, inorder, p_s+lenLeft+1, p_e, i+1, i_e);
		return node;
	}


LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值