标签:problem put get 链接 map ret from bin ems
方法一:递归
public TreeNode buildTree(int[] preorder, int[] inorder) { return buildTreeHelper(preorder, 0, preorder.length, inorder, 0, inorder.length); } private TreeNode buildTreeHelper(int[] preorder, int p_start, int p_end, int[] inorder, int i_start, int i_end) { // preorder 为空,直接返回 null if (p_start == p_end) { return null; } int root_val = preorder[p_start]; TreeNode root = new TreeNode(root_val); //在中序遍历中找到根节点的位置 int i_root_index = 0; for (int i = i_start; i < i_end; i++) { if (root_val == inorder[i]) { i_root_index = i; break; } } int leftNum = i_root_index - i_start; //递归的构造左子树 root.left = buildTreeHelper(preorder, p_start + 1, p_start + leftNum + 1, inorder, i_start, i_root_index); //递归的构造右子树 root.right = buildTreeHelper(preorder, p_start + leftNum + 1, p_end, inorder, i_root_index + 1, i_end); return root; }
作者:windliang
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--22/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
用map存储root值的位置进行优化:
public TreeNode buildTree(int[] preorder, int[] inorder) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < inorder.length; i++) { map.put(inorder[i], i); } return buildTreeHelper(preorder, 0, preorder.length, inorder, 0, inorder.length, map); } private TreeNode buildTreeHelper(int[] preorder, int p_start, int p_end, int[] inorder, int i_start, int i_end, HashMap<Integer, Integer> map) { if (p_start == p_end) { return null; } int root_val = preorder[p_start]; TreeNode root = new TreeNode(root_val); int i_root_index = map.get(root_val); int leftNum = i_root_index - i_start; root.left = buildTreeHelper(preorder, p_start + 1, p_start + leftNum + 1, inorder, i_start, i_root_index, map); root.right = buildTreeHelper(preorder, p_start + leftNum + 1, p_end, inorder, i_root_index + 1, i_end, map); return root; }
[LeetCode] 105. 从前序与中序遍历序列构造二叉树
标签:problem put get 链接 map ret from bin ems
原文地址:https://www.cnblogs.com/doyi111/p/12940431.html