Construct Binary Tree from Preorder and Inorder Traversal

原创 2016年08月30日 12:37:08

一、问题描述

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

二、思路

二叉树的题目用递归做是最便捷的,本题也不例外。

递归的退出条件是:

       if(pre_begin > preorder.size() - 1 || in_begin > in_end) return NULL;

最关键的是在先序遍历中找出当前节点的值,即root -> val。还有我们在循环中加入break,可以使我们的运行时间从20ms减少到16ms。

在执行递归的时:

左子树在先序遍历指向下一个节点,即左子树的起始位置,在中序遍历中的范围是(in_begin,index  - 1);

右子树在先序遍历指向是当前位置减去中序遍历开始的位置,即右子树的起始位置,中序遍历中的范围是(index  + 1, in_end);

三、代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return creatTree(0, 0, inorder.size() - 1, preorder, inorder);
    }
    TreeNode* creatTree(int pre_begin, int in_begin, int in_end, vector<int>& preorder, vector<int>& inorder){
       if(pre_begin > preorder.size() - 1 || in_begin > in_end) return NULL;
       TreeNode *root = new TreeNode(preorder[pre_begin]);
       int index = 0; 
       for (int i = in_end; i >= in_begin; --i) {
            if (inorder[i] == root -> val) {
                index = i;
                break;
            }
       }
       root -> left = creatTree(pre_begin + 1, in_begin, index - 1, preorder, inorder);
       root -> right = creatTree(pre_begin + index - in_begin + 1, index + 1, in_end, preorder, inorder);
       return root;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Construct Binary Tree from Preorder and Inorder Traversal LeetCode 105

题目描述: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as...
  • bu_min
  • bu_min
  • 2015年05月27日 10:22
  • 256

105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume th...
  • x_shuck
  • x_shuck
  • 2016年01月10日 22:14
  • 217

105. Construct Binary Tree from Preorder and Inorder Traversal,前序+中序 构建 树

观察:             A          /      \        B      C       /   \    D    E preorder: A B D E C ...

73.Construct Binary Tree from Preorder and Inorder Traversal-前序遍历和中序遍历树构造二叉树(中等题)

前序遍历和中序遍历树构造二叉树 题目根据前序遍历和中序遍历树构造二叉树.注意事项 你可以假设树中不存在相同数值的节点 样例给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: ...

leetcode 105-106:Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume th...

【LeetCode】Construct Binary Tree from (Preorder or Postorder) and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal

LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]

【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assu...

Construct Binary Tree from Preorder and Inorder Traversal -- LeetCode

原题链接: http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/  这...

[C++]LeetCode: 37 Construct Binary Tree from Preorder and Inorder Traversal

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assu...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Construct Binary Tree from Preorder and Inorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)