[LeetCode]*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 that duplicates do not exist in the tree.

思路

主要是根据前序遍历和中序遍历的特点解决这个题目。

1、确定树的根节点。树根是当前树中所有元素在前序遍历中最先出现的元素。
2、求解树的子树。找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元素就是右子树。若根节点左边或右边为空,则该方向子树为空;若根节点边和右边都为空,则根节点已经为叶子节点。
3、递归求解树。将左子树和右子树分别看成一棵二叉树,重复1、2、3步,直到所有的节点完成定位

代码

/*---------------------------------------
*   日期:2015-04-28
*   作者:SJF0115
*   题目: 105.Construct Binary Tree from Preorder and Inorder Traversal
*   网址:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};

class Solution {
public:
    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
        int size = preorder.size();
        if(size <= 0){
            return nullptr;
        }//if
        return PreInBuildTree(preorder,inorder,0,0,size);
    }
private:
    TreeNode* PreInBuildTree(vector<int> &preorder,vector<int> &inorder,int preIndex,int inIndex,int size){
        if(size <= 0){
            return nullptr;
        }//if
        // 根节点
        TreeNode* root = new TreeNode(preorder[preIndex]);
        // 寻找根节点在中序遍历数组的下标
        int index = 0;
        for(int i = 0;i < size;++i){
            if(preorder[preIndex] == inorder[inIndex+i]){
                index = inIndex+i;
                break;
            }//if
        }//for
        // 左子树个数
        int leftSize = index - inIndex;
        // 右子树个数
        int rightSize = size - leftSize - 1;
        // 左子树
        root->left = PreInBuildTree(preorder,inorder,preIndex+1,inIndex,leftSize);
        // 右子树
        root->right = PreInBuildTree(preorder,inorder,preIndex+1+leftSize,index+1,rightSize);
        return root;
    }
};

void PostOrder(TreeNode* root){
    if(root){
        PostOrder(root->left);
        PostOrder(root->right);
        cout<<root->val<<endl;
    }//if
}

int main(){
    Solution solution;
    vector<int> preorder = {1,2,4,8,5,3,6,7};
    vector<int> inorder = {8,4,2,5,1,6,3,7};
    TreeNode* root = solution.buildTree(preorder,inorder);
    // 输出
    PostOrder(root);
    return 0;
}

运行时间

时间: 2024-11-18 21:39:00

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal的相关文章

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. 提交成功的代码: C++实现: #include<iostream> #include<new> #include<vector> using namespace std; //Definition

[LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal

题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 思路和[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal一样. 代码 /*---------------------

Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree.   C++实现代码: #include<iostream> #include<new> #include<vector> using namespace std; //Definition for

[LeetCode]114.Flatten Binary Tree to Linked List

[题目] Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 [分析] 在先序遍历的过程中把二叉树转为链表. 用pre记住当前节点的前一节点.节点的右指针作为链表指针,同时左指针赋空(第一次wrong就是因为没赋空). pre->ri

[LeetCode]110.Balanced Binary Tree

[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. [分析] 无 [代码] /*****************

[LeetCode]--226. Invert Binary Tree

Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tre

leetcode 226 Invert Binary Tree 翻转二叉树

大牛没有能做出来的题,我们要好好做一做     Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you c

[LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

目录:1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次遍历从低往高输出 BFS 3.Maximum Depth of Binary Tree - 求二叉树的深度 DFS4.Balanced Binary Tree - 判断平衡二叉树 DFS5.Path Sum - 二叉树路径求和判断DFS 题目概述:Given a binary tree, return

[LeetCode]102.Binary Tree Level Order Traversal

[题目] Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7