【LeetCode】-Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:如何选冰箱 知乎 编辑:程序博客网 时间:2024/05/16 09:09
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
/** * 根据先序数组和中序数组,建立二叉树 * 解题思路: * 1.取先序数组的第一个元素作为根元素; * 2.找到根元素在中序数组中的位置index,小于index为左子树,大于index为右子树; * 3.分别对左右子树重复1.2操作。 * * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private int getIndex( int[] nums, int element, int start, int end ){for( int i=start; i<=end; i++ ){if( element==nums[i] )return i;}return -1;}private TreeNode createTree( int[] pre_arr,int pre_start,int[] in_arr,int in_start,int length){if( length<=0 ){return null;}//取先序数组的第一个元素,作为跟节点TreeNode root = new TreeNode(pre_arr[pre_start]);//计算根节点元素在先序数组中的下标int index = getIndex( in_arr, pre_arr[pre_start], in_start, in_start+length-1 );int len = index - in_start;//对左右子树递归求解root.left = createTree( pre_arr, pre_start+1, in_arr, in_start, len);root.right = createTree( pre_arr, pre_start+len+1, in_arr, index+1, length-len-1);return root;} public TreeNode buildTree(int[] preorder, int[] inorder) { if( preorder.length!=inorder.length )return null;return createTree( preorder, 0, inorder, 0, inorder.length ); }}
0 0
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode-Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode - Construct Binary Tree from Preorder and Inorder Traversal
- [leetCode] Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- 今天晚上的作业 我只能这样发发了!
- 蓝光片源介绍
- 笨鸟之Struts2的Interceptor模拟实现
- 客服管理系统--售后服务、商品评论管理、咨询邮件回复
- Tomcat下配置数据源链接数据库
- 【LeetCode】-Construct Binary Tree from Preorder and Inorder Traversal
- pandas小记:pandas数据规整化-分组合并及重塑
- bzoj1207:[HNOI2004]打鼹鼠[DP]
- Minimum Depth of Binary Tree (leetcode)
- yahoo 笔试题
- awk和grep退出状态问题
- Oracle 11g服务详细介绍及哪些服务是必须开启的?
- Oracle中tnsping 主机名/Ip 显示TNS-12541: TNS:no listener
- Android中最简单的分享功能