给定两个整数数组preorder和inorder,表示一个二叉树的前序遍历和中序遍历,重构出原二叉树。假设二叉树的节点值没有重复,二叉树节点的定义已经给出。
示例1
输入
[3,9,20,15,7],[9,3,15,20,7],5
输出
{3,9,20,#,#,15,7}
说明
样例结果展示
加载中...
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * 二叉树构造 * @param preorder int整型一维数组 前序遍历 * @param inorder int整型一维数组 中序遍历 * @param length int整型 节点数量 * @return TreeNode类 */ public TreeNode constructTree (int[] preorder, int[] inorder, int length) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * 二叉树构造 * @param preorder int整型一维数组 前序遍历 * @param preorderLen int preorder数组长度 * @param inorder int整型一维数组 中序遍历 * @param inorderLen int inorder数组长度 * @param length int整型 节点数量 * @return TreeNode类 */ TreeNode* constructTree(int* preorder, int preorderLen, int* inorder, int inorderLen, int length) { // write code here } };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 二叉树构造 # @param preorder int整型一维数组 前序遍历 # @param inorder int整型一维数组 中序遍历 # @param length int整型 节点数量 # @return TreeNode类 # class Solution: def constructTree(self , preorder , inorder , length ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 二叉树构造 * @param preorder int整型一维数组 前序遍历 * @param inorder int整型一维数组 中序遍历 * @param length int整型 节点数量 * @return TreeNode类 */ function constructTree( preorder , inorder , length ) { // write code here } module.exports = { constructTree : constructTree };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 二叉树构造 # @param preorder int整型一维数组 前序遍历 # @param inorder int整型一维数组 中序遍历 # @param length int整型 节点数量 # @return TreeNode类 # class Solution: def constructTree(self , preorder , inorder , length ): # write code here
[3,9,20,15,7],[9,3,15,20,7],5
{3,9,20,#,#,15,7}