LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
2015-11-26 11:37
441 查看
题目描述
Given preorder and inorder traversal of a tree, construct the binary tree.Note:
You may assume that duplicates do not exist in the tree.
代码
[code] int p = 0; int[] preorder; int[] inorder; public TreeNode buildTree(int[] preorder, int[] inorder) { this.preorder = preorder; this.inorder = inorder; return buildTree(0, preorder.length); } TreeNode buildTree(int start, int end) { if (start >= end) { return null; } TreeNode root = new TreeNode(preorder[p]); int i; for (i = start; i < end && preorder[p] != inorder[i]; i++) ; p++; root.left = buildTree(start, i); root.right = buildTree(i + 1, end); return root; }
相关文章推荐
- java中try-catch-finally
- 上传图片
- Linux 下JDK1.7.0_75的安装
- 这个错误,每个ScrumMaster都犯过
- Express难点解析
- csv操作帮助类
- java代码检查工具-FindBugs
- 如果要创业,你应该如何选择创业方向?
- Memcached安装配置
- svn服务搭建
- 自定义UINavigationController 切换动画
- Bootstrap每天必学之按钮
- Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
- Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
- Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live
- app评论
- UltraEdit相关设置
- HTML与JSP页面的区别
- pymol png python 不输出图片 ray解决方法 ,
- opengl绘制出现锯齿的一个可能原因