LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(从前序遍历和中序遍历构造二叉树)
来源:互联网 发布:在淘宝点击卖家没反应 编辑:程序博客网 时间:2024/05/21 01:31
原题网址:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
方法:自顶向下,根据前序遍历和中序遍历的特性构造,通过哈希映射快速确定节点在某个遍历中的位置。
前序遍历总是先出现根节点,所以根据前序遍历就可以自顶向下构造,先构造根节点。中序遍历可以根据根节点,将树分为左右子树。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private Map<Integer, Integer> imap = new HashMap<>(); private TreeNode build(int[] preorder, int pfrom, int pto, int[] inorder, int ifrom, int ito) { if (pfrom >= pto) return null; TreeNode root = new TreeNode(preorder[pfrom]); int leftLength = imap.get(preorder[pfrom]) - ifrom; TreeNode left = build(preorder, pfrom+1, pfrom+1+leftLength, inorder, ifrom, ifrom+leftLength); TreeNode right = build(preorder, pfrom+1+leftLength, pto, inorder, ifrom+leftLength+1, ito); root.left = left; root.right = right; return root; } public TreeNode buildTree(int[] preorder, int[] inorder) { for(int i=0; i<inorder.length; i++) imap.put(inorder[i], i); return build(preorder, 0, preorder.length, inorder, 0, inorder.length); }}
0 0
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(从前序遍历和中序遍历构造二叉树)
- [LintCode] 前序遍历和中序遍历树构造二叉树 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 (根据前序遍历和中序遍历构造二叉树)
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- 73.Construct Binary Tree from Preorder and Inorder Traversal-前序遍历和中序遍历树构造二叉树(中等题)
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序遍历构建二叉树
- LeetCode 106. 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 中序遍历和后序遍历树构造二叉树
- 105.leetcode Construct Binary Tree from Preorder and Inorder Traversal(medium)[先序、中序构造二叉树]
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal(根据前序、中序遍历确定一棵二叉树 C语言)
- Leetcode Construct Binary Tree from Preorder and Inorder Traversal 前序中序遍历重组二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- LeetCode OJ 之 Construct Binary Tree from Preorder and Inorder Traversal (由二叉树的前序和中序序列构造二叉树)
- leetcode #23 in cpp
- Leetcode 130. Surrounded Regions
- LeetCode 102. Binary Tree Level Order Traversal(二叉树分层遍历)
- LeetCode 103. Binary Tree Zigzag Level Order Traversal(二叉树之字形遍历)
- LeetCode 104. Maximum Depth of Binary Tree(二叉树高度)
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(从前序遍历和中序遍历构造二叉树)
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构造二叉树)
- LeetCode 107. Binary Tree Level Order Traversal II(二叉树分层遍历)
- LeetCode 108. Convert Sorted Array to Binary Search Tree(数组转换为二叉搜索树)
- LeetCode 109. Convert Sorted List to Binary Search Tree(链表到二叉搜索树)
- leetcode #24 in cpp
- Leetcode 133. Clone Graph & 138. Copy List with Random Pointer
- LeetCode 110. Balanced Binary Tree(平衡二叉树)
- LeetCode 111. Minimum Depth of Binary Tree(二叉树最小深度)