我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的。

初始化时候二叉树为:==================

中序遍历序列,           ======O===========

后序遍历序列,           =================O

红色部分是左子树,黑色部分是右子树,O是根节点

如上图所示,O是根节点,由后序遍历可知,

根据这个O可以把找到其在中序遍历当中的位置,进而,知道当前这个根节点O的左子树的前序遍历和中序遍历序列的范围。

以及右子树的前序遍历和中序遍历序列的范围。

到这里返现出现了重复的子问题,而且子问题的规模没有原先的问题大,即红色部分和黑色部分

而联系这两个子问题和原先的大问题的纽带是这个找到的根节点。

可以选择用递归来解决这个问题,递归的结束条件是子问题序列里面只有一个元素。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

给定一个二叉树的中序和后序遍历序列,构造这个二叉树。

笔记:

你可以假定,这棵树里面没有重复的节点。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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

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

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 
test.cpp:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 * int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
TreeNode *build(vector<int> &inorder, int left1, int right1, vector<int> &postorder, int left2, int right2)
{

if(right1 - left1 != right2 - left2)
    {
        return NULL;
    }
    if(right1 >= inorder.size() || right2 >= postorder.size())
    {
        return NULL;
    }

//递归结束条件
    if(left1 == right1 && left2 == right2)
    {
        TreeNode *root = new TreeNode(inorder[left1]);
        return root;
    }
    else if(left1 < right1 && left2 < right2)
    {

TreeNode *root = new TreeNode(postorder[right2]);
        int i;
        for(i = right1; i >= left1; i--)
        {
            //找到中序遍历的根节点
            if(inorder[i] == postorder[right2])
            {
                break;
            }
        }
        if(i < left1)
        {
            return NULL;
        }
        root->left = build(inorder, left1, i - 1, postorder, left2, right2 + i - right1 - 1);
        root->right = build(inorder, i + 1, right1, postorder, right2 + i - right1, right2 - 1);
        return root;

}
    else
    {
        return NULL;
    }

}

TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
    return build(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
}

vector<vector<int> > levelOrder(TreeNode *root)
{

vector<vector<int> > matrix;
    if(root == NULL)
    {
        return matrix;
    }
    vector<int> temp;
    temp.push_back(root->val);
    matrix.push_back(temp);

vector<TreeNode *> path;
    path.push_back(root);

int count = 1;
    while(!path.empty())
    {
        TreeNode *tn = path.front();
        if(tn->left)
        {
            path.push_back(tn->left);
        }
        if(tn->right)
        {
            path.push_back(tn->right);
        }
        path.erase(path.begin());
        count--;

if(count == 0)
        {
            vector<int> tmp;
            vector<TreeNode *>::iterator it = path.begin();
            for(; it != path.end(); ++it)
            {
                tmp.push_back((*it)->val);
            }
            if(tmp.size() > 0)
            {
                matrix.push_back(tmp);
            }
            count = path.size();
        }
    }
    return matrix;
}

// 树中结点含有分叉,
//                  6
//              /       \
//             7         2
//           /   \
//          1     4
//               / \
//              3   5
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(7);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(3);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(5);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

int in[7] = {1, 7, 3, 4, 5, 6, 2};
    int post[7] = {1, 3, 5, 4, 7, 2, 6};
    vector<int> inorder(in, in + 7), postorder(post, post + 7);

TreeNode *root = buildTree(inorder, postorder);

vector<vector<int> > ans = levelOrder(root);

for (int i = 0; i < ans.size(); ++i)
    {
        for (int j = 0; j < ans[i].size(); ++j)
        {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
    DestroyTree(root);
    return 0;
}

结果输出:
6
7 2
1 4
3 5
ps.测试的输出用的是层次遍历
 
BinaryTree.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_

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

TreeNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(TreeNode *pParent,
                      TreeNode *pLeft, TreeNode *pRight);
void PrintTreeNode(TreeNode *pNode);
void PrintTree(TreeNode *pRoot);
void DestroyTree(TreeNode *pRoot);

#endif /*_BINARY_TREE_H_*/

BinaryTree.cpp:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
#include <iostream>
#include <cstdio>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

//创建结点
TreeNode *CreateBinaryTreeNode(int value)
{
    TreeNode *pNode = new TreeNode(value);

return pNode;
}

//连接结点
void ConnectTreeNodes(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight)
{
    if(pParent != NULL)
    {
        pParent->left = pLeft;
        pParent->right = pRight;
    }
}

//打印节点内容以及左右子结点内容
void PrintTreeNode(TreeNode *pNode)
{
    if(pNode != NULL)
    {
        printf("value of this node is: %d\n", pNode->val);

if(pNode->left != NULL)
            printf("value of its left child is: %d.\n", pNode->left->val);
        else
            printf("left child is null.\n");

if(pNode->right != NULL)
            printf("value of its right child is: %d.\n", pNode->right->val);
        else
            printf("right child is null.\n");
    }
    else
    {
        printf("this node is null.\n");
    }

printf("\n");
}

//前序遍历递归方法打印结点内容
void PrintTree(TreeNode *pRoot)
{
    PrintTreeNode(pRoot);

if(pRoot != NULL)
    {
        if(pRoot->left != NULL)
            PrintTree(pRoot->left);

if(pRoot->right != NULL)
            PrintTree(pRoot->right);
    }
}

void DestroyTree(TreeNode *pRoot)
{
    if(pRoot != NULL)
    {
        TreeNode *pLeft = pRoot->left;
        TreeNode *pRight = pRoot->right;

delete pRoot;
        pRoot = NULL;

DestroyTree(pLeft);
        DestroyTree(pRight);
    }
}

 
 

【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】的更多相关文章

  1. leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  2. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

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

  3. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

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

  4. [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 ...

  5. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | 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 ...

  6. 106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树

    给定一棵树的中序遍历与后序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出中序遍历 = [9,3,15,20,7]后序遍历 = [9,15,7,20,3]返回如下的二叉树:    ...

  7. 106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树

    [抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assum ...

  8. (二叉树 递归) 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 ...

  9. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)

    根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...

随机推荐

  1. DLX精确覆盖与重复覆盖模板题

    hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...

  2. 让WebRTC支持H264编解码

    近期实验了下怎样让WebRTC支持H264编码.记录下,供有须要的人參考. 说明一下,我是在 Ubuntu Server 14.04 下编译的 WebRTC ,使用 native(C++) api 开 ...

  3. 浅谈<持续集成、持续交付、持续部署>(二)

    集成是指软件个人研发的部分向软件整体部分交付,以便尽早发现个人开发部分的问题:部署是代码尽快向可运行的开发/测试节交付,以便尽早测试:交付是指研发尽快向客户交付,以便尽早发现生产环境中存在的问题.如果 ...

  4. MATLAB循环结构:for语句+定积分实例

    for语句 格式: for 循环变量=表达式1:表达式2:表达式3 循环体语句 end 表达式1:循环变量初值:表达式2:步长:表达式3:循环变量终值. for 循环变量=矩阵表达式 循环体语句 en ...

  5. springboot工程自动生成工具

    1 springboot工程自动生成网址 http://start.spring.io/ 2 工具 Spring Boot CLI

  6. php输出缓冲区

    ob_start(); echo 'aaa'; $string = ob_get_contents(); file_put_contents('a.html', $string); ob_flush( ...

  7. ASP向上取整

    <%Function Ceil(value)    Dim return    return = int(value)    Cei2=value-return    if Cei2>0 ...

  8. java堆分析神器MAT

    Memory Analyzer(MAT) 基于Eclipse的软件 http://www.eclipse.org/mat/

  9. Symfony 安装FOUSerBundle

    第一按照官网安装 : https://symfony.com/doc/current/bundles/FOSUserBundle/index.html#main 可能版本无法安装 : $ compos ...

  10. R语言数据管理(五)

    一.数据的输入: 手动输入:edit( )函数 也可修改 mydata <- data.frame(age=numeric(0),gender=character(0),weight=numer ...