题解 | #重建二叉树#
重建二叉树
https://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param preOrder int整型一维数组 * @param vinOrder int整型一维数组 * @return TreeNode类 */ public TreeNode reConstructBinaryTree (int[] preOrder, int[] vinOrder) { // write code here if(preOrder.length == 0){ return null; } int root = preOrder[0]; TreeNode treeNode = new TreeNode(root); for(int i = 0;i < vinOrder.length; i++){ if(preOrder[0] == vinOrder[i]){ treeNode.left = reConstructBinaryTree(Arrays.copyOfRange(preOrder,1,i+1),Arrays.copyOfRange(vinOrder,0,i)); treeNode.right = reConstructBinaryTree(Arrays.copyOfRange(preOrder,i+1,preOrder.length),Arrays.copyOfRange(vinOrder,i+1,vinOrder.length)); break; } } return treeNode; } }