[LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
2015-12-06 05:19
471 查看
public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { int length = preorder.length; if (length == 0) { return null; } TreeNode root = new TreeNode(preorder[0]); int i = 0; for(; i < length; i++) { if (inorder[i] == root.val) { break; } } root.left = buildTree(Arrays.copyOfRange(preorder, 1, 1 + i), Arrays.copyOfRange(inorder, 0, i)); root.right = buildTree(Arrays.copyOfRange(preorder, 1 + i, length), Arrays.copyOfRange(inorder, 1 + i, length)); return root; } }
相关文章推荐
- ExtJS4.2学习(20)动态数据表格之前几章总结篇1(转)
- 对System.ComponentModel.DataAnnotations 的学习应用
- 【BLE】CC2541之SNV
- css3学习笔记(一)
- Angular JS 学习笔记四
- UVALive 6840 Sweet War (博弈dp)
- UVALive 6838 Flipping Parentheses (线段树)
- UVALive 6837 There is No Alternative (MST + LCA)
- UVALive 6833 Miscalculation (模拟)
- UVALive 6832 Bit String Reordering (模拟)
- django最简单表单入门
- UVALive 6834 Shopping (贪心)
- [LeetCode306] Additive Number
- [python爬虫] Selenium定向爬取PubMed生物医学摘要信息
- mvc项目架构搭建之UI层的搭建
- aJax提交——服务端不能用request存储数据,session存数据客户端可以接收到
- 最简单的原型继承
- 268. Missing Number
- JS 用window.open()函数,父级页面如何取到子级页面的返回值?
- Use Avahi as a library for Discovering mDNS Service