[LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:第三方抽奖软件 编辑:程序博客网 时间:2024/05/29 08:03
根据前序遍历和中序遍历树构造二叉树.
注意事项
你可以假设树中不存在相同数值的节点.
样例
给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树:
2
/ \
1 3
Given preorder and inorder traversal of a tree, construct the binary tree.
Notice
You may assume that duplicates do not exist in the tree.
Example
Given in-order [1,2,3] and pre-order [2,1,3], return a tree:
2
/ \
1 3
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /** *@param preorder : A list of integers that preorder traversal of a tree *@param inorder : A list of integers that inorder traversal of a tree *@return : Root of a tree */ public TreeNode buildTree(int[] preorder, int[] inorder) { if(null == preorder || null == inorder || preorder.length != inorder.length || preorder.length == 0) return null; return createTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1); } public TreeNode createTree(int[] preorder, int lp, int rp, int[] inorder, int li, int ri) { if(lp > rp || li > ri) return null; TreeNode root = new TreeNode(preorder[lp]); int inlo = location(inorder, preorder[lp]); root.left = createTree(preorder, lp+1, lp+inlo-li,inorder, li, inlo-1); root.right = createTree(preorder, lp+inlo-li+1, rp,inorder, inlo+1, ri); return root; } public int location(int[] nums, int key) { int i = 0; while(nums[i++] != key); return --i; }}
0 0
- [LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal 根据前序遍历和中序遍历构造二叉树
- [LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- lintcode construct-binary-tree-from-inorder-and-postorder-traversal 中序遍历和后序遍历树构造二叉树
- 73.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 前序和中序遍历构建二叉树
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序重建二叉树
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(从前序遍历和中序遍历构造二叉树)
- LeetCode105--Tree--Construct Binary Tree from Preorder and Inorder Traversal--根据前序遍历+中序遍历构建树
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal(根据前序、中序遍历确定一棵二叉树 C语言)
- LeetCode OJ 之 Construct Binary Tree from Preorder and Inorder Traversal (由二叉树的前序和中序序列构造二叉树)
- 根据前序和中序序列重建二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal-先序和中序构造树
- Construct Binary Tree from Inorder and Postorder Traversal 利用中序遍历和后序遍历构造树
- SVM核技巧的经典解释
- 每天一篇python:简单爬虫下载图片篇
- 常见linux命令
- 第7周项目2-友元类
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- [LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- AngularJS最理想开发工具WebStorm
- 制作图片数字 [代码清单10-3]
- leetcode_088 Merge Sorted Array
- 安卓学习笔记获取手机内存与SD卡路径及大小
- 用隐藏域判断页面是否第一次登陆
- 使用 udev 高效、动态地管理 Linux 设备文件
- C 语言static
- Maven配置+eclipse创建Maven项目