[LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:ps for mac破解版下载 编辑:程序博客网 时间:2024/05/29 06:54
根据中序遍历和后序遍历树构造二叉树
注意事项
你可以假设树中不存在相同数值的节点
样例
给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2]
返回如下的树:
2
/ \
1 3
Given inorder and postorder traversal of a tree, construct the binary tree.
Notice
You may assume that duplicates do not exist in the tree.
Example
Given inorder [1,2,3] and postorder [1,3,2], 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 inorder : A list of integers that inorder traversal of a tree *@param postorder : A list of integers that postorder traversal of a tree *@return : Root of a tree */ public TreeNode buildTree(int[] inorder, int[] postorder) { if(null == inorder || null == postorder || inorder.length != postorder.length || inorder.length <= 0) return null; return createTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1); } public TreeNode createTree(int[] inorder, int il, int ir, int[] postorder, int pl,int pr) { if(il > ir || pl > pr) return null; TreeNode root = new TreeNode(postorder[pr]); int inlo = searchArray(inorder, postorder[pr]); root.left = createTree(inorder, il, inlo-1, postorder, pl,pl+inlo-il-1); root.right = createTree(inorder, inlo+1, ir, postorder, pl+inlo-il, pr-1); return root; } public int searchArray(int[] nums, int key) {//key一定在nums内 int i = 0; while(nums[i++] != key); return --i; }}
0 0
- [LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- lintcode construct-binary-tree-from-inorder-and-postorder-traversal 中序遍历和后序遍历树构造二叉树
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 利用中序遍历和后序遍历构造树
- [LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- 72.Construct Binary Tree from Inorder and Postorder Traversal-中序遍历和后序遍历树构造二叉树(中等题)
- leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构造二叉树)
- 【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序重建二叉树
- 利用二叉树的中序遍历和后序遍历序列构造一个二叉树Search results for Construct Binary Tree from Inorder and Postorder Traversa
- Construct Binary Tree from Preorder and Inorder Traversal 根据前序遍历和中序遍历构造二叉树
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- 106.leetcode.Construct Binary Tree from Inorder and Postorder Traversal(medium)[中序、后序构造二叉树]
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal (利用中序遍历和后续遍历确定一颗二叉树)
- 第八周项目1:数组做数据成员(1)
- HDU2084 DP + 水 /入门+ 递推 + 从后往前思考问题的方式
- 安卓012Activity生命周期
- Hibernate-Session使用的背后
- Java基础(3)
- [LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- Cuda学习笔记(四)——在vs2010中配置.cpp和.cu文件
- spring配置文件详解--真的蛮详细
- 一张正则表达式的图,mark一下
- php连接mysql数据库
- java中作用域的问题
- Android问题之this与Activity.this的区别
- 最小生成树--Kruskal算法
- TCP/IP学习笔记(三)——IP,ARP,RARP