【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】
2015-08-09 06:37
836 查看
【105-Construct Binary Tree from Preorder and Inorder Traversal(通过前序和中序遍历构造二叉树)】
【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
原题
Given preorder and inorder traversal of a tree, construct the binary tree.Note:
You may assume that duplicates do not exist in the tree.
题目大意
给定一个前序和中序遍历序列,构建一个二叉树注意:
- 二叉树中元素重复元素
解题思路
前序遍历第一个元素是根结点(k),在中序遍历序列中找值为k的下标idx,idx将中序遍历序列分成左右子树,对前序遍历序列也一样,可进行递归操作代码实现
树结点类public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }
算法实现类一:
public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if (preorder == null || preorder.length ==0){ return null; } HashMap<Integer, Integer> inorderMap = new HashMap<Integer, Integer>(); for (int i=0;i<inorder.length;i++){ inorderMap.put(inorder[i],i); } Deque<TreeNode> stack = new LinkedList<TreeNode>(); TreeNode root = new TreeNode(preorder[0]); stack.push(root); for (int i=1;i<preorder.length;i++){ TreeNode top = stack.peek(); int indexTop = inorderMap.get(top.val); int indexVal = inorderMap.get(preorder[i]); TreeNode node = new TreeNode(preorder[i]); if (indexVal<indexTop){ top.left=node; } else{ while (indexVal>indexTop){ top = stack.pop(); indexTop = stack.isEmpty()?Integer.MAX_VALUE:inorderMap.get(stack.peek().val); } top.right = node; } stack.push(node); } return root; } }
算法实现类二:(会超时)
public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { // 参数校验 if (preorder == null || inorder == null || preorder.length == 0 || preorder.length != inorder.length) { return null; } return solve(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1); } /** * 构建二叉树,数据输入的正确性由输入数据自己保证 * * @param preorder 先序遍历的结果 * @param x 先序遍历的开始位置 * @param y 先序遍历的结束位置 * @param inorder 中序遍历的结果 * @param i 中序遍历的开始位置 * @param j 中序遍历的结束位置 * @return 二叉树的根结点 */ public TreeNode solve(int[] preorder, int x, int y, int[] inorder, int i, int j) { if (x >= 0 && x <= y && i >= 0 && i <= j) { // 只有一个元素 if (x == y) { return new TreeNode(preorder[x]); } else if (x < y) { // 记录根结点的索引 int idx = i; while (idx <= j && inorder[idx] != preorder[x]) { idx++; } // 创建根结点 TreeNode root = new TreeNode(inorder[idx]); // 左子树的结点个数 int leftLength = idx - i; // if (leftLength > 0) { // x + 1, x + leftLength:左子树起始和结束位置 root.left = solve(preorder, x + 1, x + leftLength, inorder, i, idx - 1); } // 右子树的结点个数 int rightLength = j - idx; if (rightLength > 0) { // x + leftLength + 1, y:右子树起始和结束位置 root.right = solve(preorder, x + leftLength + 1, y, inorder, idx + 1, j); } return root; } } return null; } }
评测结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。特别说明
欢迎转载,转载请注明出处【/article/1343389.html】
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【070-Set Matrix Zeroes(矩阵置零)】
- 黑马程序员——二维数组-第7天
- 【剑指Offer面试题】 九度OJ1504:把数组排成最小的数
- 黑马程序员--java概述
- 证明你是坏程序员的7个迹象
- 轻松搞定面试中的链表题目
- 据说年薪30万的Android程序员必须知道的帖子
- 轻松搞定面试中的红黑树问题
- 一个码农对软件项目基层人员管理的思考——会议篇
- 黑马程序员--学习OC Foundation1
- 黑马程序员 oc随记 类方法与对象方法
- 剑指Offer面试题31(java版):连续子数组的最大和
- 黑马程序员---OC Category
- 黑马程序员--java基础--基础语法
- 黑马程序员——Java基础---IO(二)---IO字节流、流操作规律
- 黑马程序员--IO流之字节流
- StackOverflow程序员推荐:每个程序员都应读的30本书
- 《程序员面试宝典3》大量错误(50+)纠正表
- Java后台工程师面试杂记——不跳不涨工资星人跳槽经历
- 剑指offer_面试题12_打印1到最大的n位数(大数问题)