leetcode Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:程序员的发展趋势 编辑:程序博客网 时间:2024/06/10 14:11
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
后序遍历的最后一个节点必为根节点,而中序遍历的根节点处的左半部分和右半部分必为左子树和右子树,根据这个性质递归即可,代码:
public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder==null||inorder.length==0||postorder==null||postorder.length==0) return null; if(inorder.length==1) return new TreeNode(inorder[0]); TreeNode root=new TreeNode(postorder[postorder.length-1]); int index=0; for(int i=0;i<inorder.length;i++){ if(inorder[i]==root.val){ index=i; break; } } root.left=buildTree(Arrays.copyOfRange(inorder,0,index),Arrays.copyOfRange(postorder,0,index)); root.right=buildTree(Arrays.copyOfRange(inorder,index+1,postorder.length),Arrays.copyOfRange(postorder,index,postorder.length-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
- js 窗口操作
- HDU 1561(树形dp)
- eclipse启动项目非常慢
- poj 2455 Secret Milking Machine (网络流)
- 找连续数 HDU5247
- leetcode Construct Binary Tree from Inorder and Postorder Traversal
- redis基本命令
- android适配
- 归并排序
- tcp服务器与客户端
- list,set练习和Map简述
- wxWidgets界面设计工具DialogBlocks
- 使用C++11实现类似C#的属性概念设计
- Jackson之JSON包的使用分析