7、重建二叉树

xiaoxiao2021-02-28  45

import java.util.Arrays; /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode reConstructBinaryTree(int [] pre,int [] in) { if((pre.length<=0) || (in.length<=0)) return null; TreeNode root = new TreeNode(pre[0]); int index; for(index=0;index<in.length;index++) { if(in[index]==pre[0]) break; } root.left = reConstructBinaryTree(Arrays.copyOfRange(pre,1,index+1),Arrays.copyOfRange(in,0,index)); root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,index+1,in.length),Arrays.copyOfRange(in,index+1,in.length)); return root; } }
转载请注明原文地址: https://www.6miu.com/read-2627471.html

最新回复(0)