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.
题目大意
根据前序和中序,构建二叉树。
解题思路
由于前序遍历的规则是:根->左->右,所以前序遍历的第一个元素为根节点,根据这一特性,将中序数组分为左子树和右子树。重复以上操作。
代码
|
|
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.
题目大意
根据后序和中序,构建二叉树
解题思路
与前序和中序构建树的思路差不多。
需要使用后序特性,最后一个数为根节点。将前序数组分为左右两个子树。
代码
|
|