Construct Binary Tree from Inorder and Postorder Traversal 利用中序遍历和后序遍历构造树
来源:互联网 发布:猪八戒考试题库软件 编辑:程序博客网 时间:2024/05/15 00:16
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
利用中序遍历和后序遍历构造树,跟前面的博文,利用前序遍历和中序遍历构造树是类似的。
先来构造一颗二叉树。
中序遍历为:9,7,10,4,8,13
后序遍历为:9,10,7,13,8,4
这里是后序遍历划分中序遍历。
差别有2点:
1. 后序遍历的最后一个节点为root
2. root节点4,后面是8,8 是right部分的。所以这里是先构造右子树,再构造左子树。
运行时间:
代码:
ublic class ConstructBinaryTreefromInorderandPostorderTraversal { private int postIndex = 0; private Map<Integer, Integer> valueToIndex = new HashMap<>(); public TreeNode buildTree(int[] inorder, int[] postorder) { for (int i = 0; i < inorder.length; i++) { valueToIndex.put(inorder[i], i); } postIndex = postorder.length - 1; return doBuildTree(0, inorder.length - 1, postorder); } private TreeNode doBuildTree(int inBeigin, int inEnd, int[] postorder) { if (inBeigin > inEnd) { return null; } int mid = valueToIndex.get(postorder[postIndex]); TreeNode root = new TreeNode(postorder[postIndex]); postIndex--; root.right = doBuildTree(mid + 1, inEnd, postorder); root.left = doBuildTree(inBeigin, mid - 1, postorder); return root; }}
1 0
- Construct Binary Tree from Inorder and Postorder Traversal 利用中序遍历和后序遍历构造树
- [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 中序和后序遍历二叉树
- 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(根据中序遍历和后序遍历构造二叉树)
- 利用二叉树的中序遍历和后序遍历序列构造一个二叉树Search results for Construct Binary Tree from Inorder and Postorder Traversa
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- 【LeetCode笔记】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 中序和后序重建二叉树
- [LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal 根据前序遍历和中序遍历构造二叉树
- 106.leetcode.Construct Binary Tree from Inorder and Postorder Traversal(medium)[中序、后序构造二叉树]
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- RatingBar属性详解 - Android 中文 API (40) —— RatingBar
- PhotoShop CS6 抠图教程
- 安卓之相机Camera(二)
- jad反编译class类文件的时候容易出错的几种情况
- hadoop 安装教程
- Construct Binary Tree from Inorder and Postorder Traversal 利用中序遍历和后序遍历构造树
- 全面解读Java NIO工作原理(一)
- 苹果手机上传图片之后竖图倾斜
- 更改UITextField的placeholder文字颜色的5种方法 及placeholder字体大小
- Uiautomator向jar包传多个参数
- poj之旅——2549
- The .NET Framework for Users(服务于用户的.net框架)
- 在北京租房子需要注意什么?
- Android EditText不自动弹出软键盘