construct-binary-tree-from-inorder-and-postorder-traversal Java code
来源:互联网 发布:内蒙古网络春晚 编辑:程序博客网 时间:2024/06/15 03:07
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 { public TreeNode buildTree(int[] inorder, int[] postorder) { int len = postorder.length; if(len == 0)return null; int root_val = postorder[len - 1]; TreeNode root = new TreeNode(root_val); int root_index; for(root_index = 0; root_index < len; root_index++){ if(inorder[root_index] == root_val)break; } int left_len =root_index; int right_len = len - root_index - 1; int[] left_in = new int[left_len]; int[] left_post = new int[left_len]; int[] right_in = new int[right_len]; int[] right_post = new int[right_len]; for(int i = 0; i < root_index; i++){ left_in[i] = inorder[i]; left_post[i] = postorder[i]; } for(int i = root_index + 1; i < len; i++){ right_in[i - root_index - 1] = inorder[i]; } for(int i = root_index; i < len - 1; i++){ right_post[i - root_index] = postorder[i]; } TreeNode left = buildTree(left_in, left_post); TreeNode right = buildTree(right_in, right_post); root.left = left; root.right = right; return root; }}
阅读全文
0 0
- construct-binary-tree-from-inorder-and-postorder-traversal Java code
- CODE 27: Construct Binary Tree from Inorder and Postorder Traversal
- [leet code] Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal (Java)
- Construct Binary Tree from Inorder and Postorder Traversal Java
- Construct Binary Tree from Inorder and Postorder Traversal (Java)
- [JAVA]LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode][Java] Construct Binary Tree from Inorder and Postorder Traversal
- construct-binary-tree-from-preorder-and-inorder-traversal Java code
- *(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
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- python文件操作
- IntelliJ IDEA下使用 mybatis-generator-maven-plugin插件完成Mybatis逆向工程
- 百度地图设置缩放比例
- ACE安装
- css的字体设置,css的盒子模型,css的盒子模型的边框,盒子模型的内边距属性,盒子模型的外边距属性,
- construct-binary-tree-from-inorder-and-postorder-traversal Java code
- Say Cheese UVA
- Codeforces Round #397 F. Souvenir线段树套set
- listen EADDRINUSE 127.0.0.1:3000
- 荐号|10个优质的大数据、人工智能公众号
- LeetCode#31. Next Permutation
- 阿里druid连接池监控配置
- pythonOpenCV之安装和第一个程序
- 微服务架构