标签:建立 hat 一个 turn tor return example color 中序遍历
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
中序、后序遍历得到二叉树,可以知道每一次新数组的最后一个数为当时子树的根节点,每次根据中序遍历的根节点的左右两边确定左右子树,再对应后序的左右子树,不停递归得到根节点,可以建立二叉树。每次由循环得到根节点在中序数组中坐标i
由中序遍历知:每一次inorder的左子树范围[ileft,i-1],右子树范围[i+1,iright]
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
标签:建立 hat 一个 turn tor return example color 中序遍历
原文地址:https://www.cnblogs.com/hhhhan1025/p/10713560.html