【LeetCode】-Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:cpi最新数据 编辑:程序博客网 时间:2024/05/01 08:02
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private int getIndex( int[] nums, int element, int start, int end ){for( int i=start; i<=end; i++ ){if( element==nums[i] )return i;}return -1;}private TreeNode createTree( int[] in_arr, int in_start, int[] post_arr, int post_start, int length ){if( length<=0 )return null;TreeNode root = new TreeNode( post_arr[post_start+length-1] );int index = getIndex( in_arr, post_arr[post_start+length-1], in_start, in_start+length );int len = index - in_start;root.left = createTree( in_arr, in_start, post_arr, post_start, len );root.right = createTree( in_arr, index+1, post_arr, post_start+len, length-len-1 );return root;} public TreeNode buildTree(int[] inorder, int[] postorder) { if( inorder.length!=postorder.length )return null;return createTree( inorder, 0, postorder, 0, inorder.length ); }}
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
- Bellman_Ford算法
- android导入工程出现红色感叹号
- oracle 序列的创建删除
- 设置Linux系统root用户密码
- eclipse中jsp配置
- 【LeetCode】-Construct Binary Tree from Inorder and Postorder Traversal
- UISearchbar去除背景色的方法,适合iOS5/6/7/8.0beta
- 图解TCP/IP协议
- Objective-C 中的CRC32验证
- linux系统监控图表展示--nmon和nmon analyser
- UNIX网络编程卷2进程间通信读书笔记-管道
- 什么是渗透测试
- location.href、parent.location.href、top.location.href、 window.open实现页面跳转
- Delphi下SPCOMM串口编程(转)