【Leetcode】Construct binary tree from inorder and postorder traversal
来源:互联网 发布:信用卡加淘宝帐号贷款 编辑:程序博客网 时间:2024/05/16 17:03
【题目】
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
【思路】
pretty similar with that of
【代码】
public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder.length == 0 || inorder.length != postorder.length) return null; return buildTree(inorder,postorder,0,inorder.length-1,0,postorder.length-1); } public TreeNode buildTree(int[] inorder, int[]postorder, int ilow, int ihigh, int plow, int phigh) { TreeNode root = new TreeNode(postorder[phigh]); int i=-1; for(i = ilow; i <= ihigh; i++) { if(root.val == inorder[i]) { break; } } if(i != ilow) root.left = buildTree(inorder, postorder, ilow, i-1, plow,plow+i-ilow-1); if(i != ihigh) root.right = buildTree(inorder,postorder, i+1, ihigh, plow+i-ilow, phigh-1); return root; }
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: 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
- LeetCode 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
- [LeetCode] 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
- leetcode 110: 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
- [leetcode]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
- LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- hdu1016
- LeetCode Valid Anagram
- hdu1015
- hdu1716
- LeetCode Sort List
- 【Leetcode】Construct binary tree from inorder and postorder traversal
- Java Difference between Private and Protected
- babyos (一)——利用BIOS 中断INT 0x10显示字符和字符串
- 高效程序的秘密_hacker's delight读书笔记
- LeetCode Convert Sorted List to Binary Search Tree
- matlab绘图时设置原点的位置
- LeetCode Convert Sorted Array to Binary Search Tree
- cyuqpcljzwrmku
- lintcode做题总结, 题目Rotate String 8