LeetCode:Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:外戚 知乎 编辑:程序博客网 时间:2024/05/21 10:25
Construct Binary Tree from Preorder and Inorder Traversal
Total Accepted: 66121 Total Submissions: 227515 Difficulty: Medium
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Subscribe to see which companies asked this question
Hide Similar Problems
思路:
根据“前序”数组的第一个结点,在“中序”数组中找到“根”结点index;根据index把“前序”分成index的左右子树。
java code:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if(preorder == null || inorder == null || preorder.length != inorder.length) return null; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i=0;i<inorder.length;i++) map.put(inorder[i], i); return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1, map); } // 自定义函数 private TreeNode buildTree(int[] preorder, int s1, int e1, int[] inorder, int s2, int e2, Map<Integer, Integer> map) { if(s1 > e1 || s2 > e2) return null; TreeNode root = new TreeNode(preorder[s1]); int rootIndex = map.get(preorder[s1]); // 在前序数组中找到根结点 root.left = buildTree(preorder, s1+1, s1+rootIndex-s2, inorder, s2, rootIndex-1, map); root.right = buildTree(preorder, s1+rootIndex-s2+1, e1, inorder, rootIndex+1, e2, map); return root; }}
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
- Android下拉列表显示
- web安全的那点事儿
- 动态存储分配及深复制——人数不定的工资类
- Android内存泄漏检测利器:LeakCanary
- php 实现长连接
- LeetCode:Construct Binary Tree from Preorder and Inorder Traversal
- [C++ 面试基础知识总结] 泛型算法
- xcode快捷键
- list、set、map集合的综合应用
- CSS 设置宽度适合屏幕
- STL源码剖析——deque
- Memcached - In Action
- 两个线程同时做100次a++
- strspn