[leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告
来源:互联网 发布:淘宝修改销量犯法 编辑:程序博客网 时间:2024/06/12 18:08
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
package go.jacob.day806;public class Demo4 {/* * 假设树的先序遍历是12453687,中序遍历是42516837。 * 这里最重要的一点就是先序遍历可以提供根的所在, * 而根据中序遍历的性质知道根的所在就可以将序列分为左右子树。 * 比如上述例子,我们知道1是根,所以根据中序遍历的结果425是左子树,而6837就是右子树。 * 接下来根据切出来的左右子树的长度又可以在先序便利中确定左右子树对应的子序列 * (先序遍历也是先左子树后右子树)。 * 根据这个流程,左子树的先序遍历和中序遍历分别是245和425, * 右子树的先序遍历和中序遍历则是3687和6837,我们重复以上方法,可以继续找到根和左右子树, * 直到剩下一个元素。 */public TreeNode buildTree(int[] preorder, int[] inorder) {return buildTree(preorder,0,preorder.length-1,inorder,0,inorder.length-1);}private TreeNode buildTree(int[] preorder, int preLeft, int preRight, int[] inorder, int inLeft, int inRight) {if(preRight<preLeft)return null;TreeNode node=new TreeNode(preorder[preLeft]);if(preRight==preLeft) return node;int num=0;for(int i=inLeft;i<=inRight;i++){if(inorder[i]==preorder[preLeft]){num=i;break;}}int length=num-inLeft;node.left=buildTree(preorder,preLeft+1,preLeft+length,inorder,inLeft,inLeft+length-1);node.right=buildTree(preorder,preLeft+length+1,preRight,inorder,num+1,inRight);return node;}}
阅读全文
0 0
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal解题报告
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal (Java)
- [LeetCode][Java] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode --- 105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] 105.Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- LeetCode#105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- openstack平台的搭建(先电版)
- scanf的几种基本用法
- android studio生成的apk闪退问题
- 好的程序员与不好的程序员
- 16.数据库对象----序列(sequence)
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告
- JS_return、return false的区别
- H5面试题---new操作符具体干了什么呢?和null和undefined的区别?
- Android奇葩说(持续更新中...)
- Backward Digit Sums(全排列next_permutation)
- 远程服务过程
- Java接口的概念
- 17.数据库对象----索引(index)
- H5---js有哪些内置对象?和介绍js的基本数据类型