新博文地址:
[leetcode]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Note:
You may assume that duplicates do not exist in the tree.
跟这道题没有任何区别。
public TreeNode buildTree(int[] inorder, int[] postorder) { if(inorder.length == 0){ return null; } if(inorder.length == 1){ return new TreeNode(inorder[0]); } TreeNode root = new TreeNode(postorder[postorder.length - 1]); int[] leftInorder = getLeftInorder(inorder,postorder[postorder.length - 1]); int[] rightInorder = Arrays.copyOfRange(inorder,leftInorder.length + 1,inorder.length); int[] leftPostorder = Arrays.copyOf(postorder,leftInorder.length); int[] rightPostorder = Arrays.copyOfRange(postorder,leftPostorder.length,postorder.length - 1); root.left = buildTree(leftInorder,leftPostorder); root.right = buildTree(rightInorder,rightPostorder); return root; } private int[] getLeftInorder(int[] inorder ,int target){ int length = 0 ; for(; length < inorder.length; length++){ if(inorder[length] == target){ break; } } return Arrays.copyOf(inorder,length); }
当树中包含重复数字该怎样
相关推荐
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
java java_leetcode题解之Construct Binary Tree from String.java
python python_leetcode题解之094_Binary_Tree_Inorder_Traversal
c语言基础 c语言_leetcode题解之0094_binary_tree_inorder_traversal.zip
javascript js_leetcode题解之94-binary-tree-inorder-traversal.js
python python_leetcode题解之145_Binary_Tree_Postorder_Traversal
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】
javascript js_leetcode题解之145-binary-tree-postorder-traversal.js
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
python python_leetcode题解之106_Construct_Binary_Tree_from_Inorder
java java_leetcode题解之Construct String from Binary Tree.java
js js_leetcode题解之106-construct-binary-tree-from-inorder
python python_leetcode题解之102_Binary_Tree_Level_Order_Traversal
python python_leetcode题解之107_Binary_Tree_Level_Order_Traversal_II
js js_leetcode题解之102-binary-tree-level-order-traversal.js
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde
java java_leetcode题解之Binary Tree Cameras.java