欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++实现LeetCode(106.由中序和后序遍历建立二叉树)

程序员文章站 2022-04-06 22:22:05
[leetcode] 106. construct binary tree from inorder and postorder traversal 由中序和后序遍历建立二叉树given inorde...

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

for example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

return the following binary tree:

    3
/ \
9  20
/  \
15   7

这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见博主之前的一篇博客 convert sorted array to binary search tree。针对这道题,由于后序的顺序的最后一个肯定是根,所以原二叉树的根结点可以知道,题目中给了一个很关键的条件就是树中没有相同元素,有了这个条件就可以在中序遍历中也定位出根节点的位置,并以根节点的位置将中序遍历拆分为左右两个部分,分别对其递归调用原函数。代码如下:

class solution {
public:
    treenode *buildtree(vector<int> &inorder, vector<int> &postorder) {
        return buildtree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
    }
    treenode *buildtree(vector<int> &inorder, int ileft, int iright, vector<int> &postorder, int pleft, int pright) {
        if (ileft > iright || pleft > pright) return null;
        treenode *cur = new treenode(postorder[pright]);
        int i = 0;
        for (i = ileft; i < inorder.size(); ++i) {
            if (inorder[i] == cur->val) break;
        }
        cur->left = buildtree(inorder, ileft, i - 1, postorder, pleft, pleft + i - ileft - 1);
        cur->right = buildtree(inorder, i + 1, iright, postorder, pleft + i - ileft, pright - 1);
        return cur;
    }
};

上述代码中需要小心的地方就是递归是 postorder 的左右 index 很容易写错,比如 pleft + i - ileft - 1, 这个又长又不好记,首先我们要记住 i - ileft 是计算 inorder 中根节点位置和左边起始点的距离,然后再加上 postorder 左边起始点然后再减1。我们可以这样分析,如果根结点就是左边起始点的话,那么拆分的话左边序列应该为空集,此时 i - ileft 为0, pleft + 0 - 1 < pleft, 那么再递归调用时就会返回 null, 成立。如果根节点是左边起始点紧跟的一个,那么 i - ileft 为1, pleft + 1 - 1 = pleft,再递归调用时还会生成一个节点,就是 pleft 位置上的节点,为原二叉树的一个叶节点。

下面来看一个例子, 某一二叉树的中序和后序遍历分别为:

inorder:    11  4  5  13  8  9

postorder:  11  4  13  9  8  5  

11  4  5  13  8  9      =>          5

11  4  13  9  8  5                /  \

11  4     13   8  9      =>         5

11  4     13  9  8                  /  \

                             4   8

11       13    9        =>         5

11       13    9                    /  \

                             4   8

                            /    /     \

                           11    13    9

github 同步地址:

类似题目:

construct binary tree from preorder and postorder traversal

construct binary tree from preorder and inorder traversal

参考资料:

https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/758462/c%2b%2b-detail-explain-or-diagram

https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/34803/sharing-my-straightforward-recursive-solution

到此这篇关于c++实现leetcode(106.由中序和后序遍历建立二叉树)的文章就介绍到这了,更多相关c++实现由中序和后序遍历建立二叉树内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!