【IT笔试面试题整理】有序数组生成最小高度二叉树

【试题描述】定义一个函数,输入一个有序数组生成最小高度二叉树

We will try to create a binary tree such that for each node, the number of nodes in the left
subtree and the right subtree are equal, if possible
Algorithm:
1   Insert into the tree the middle element of the array
2   Insert (into the left subtree) the left subarray elements
3   Insert (into the right subtree) the right subarray elements
4   Recurse

【参考代码】

 1     public static Node addToTree(int[] arr,int start,int end)
 2     {
 3         if(end < start)
 4             return null;
 5         int mid = (start + end)/2;
 6         Node n = new Node(arr[mid]);
 7         n.left = addToTree(arr,start,mid-1);
 8         n.right = addToTree(arr,mid+1,end);
 9         return n;
10     }
11     public static Node createMinBST(int[] array)
12     {
13         return addToTree(array,0,array.length - 1);
14     }

 

转载于:https://www.cnblogs.com/WayneZeng/archive/2013/04/11/3015317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用递归算法来生成二叉树。 具体实现步骤如下: 1. 从前序遍历数组的第一个元素开始,找到它在中序遍历数组中的位置。该元素即为当前根节点。 2. 在中序遍历数组中,该元素左边的所有元素将是当前根节点的左子树。右边的元素将是当前根节点的右子树。 3. 在前序遍历数组中,从该元素的下一个位置开始,分别为左子树的根节点和右子树的根节点。 4. 递归地将前序遍历数组中的剩余部分和中序遍历数组中相应的部分传递给递归调用,构造左子树和右子树。 下面是typescript代码示例: ``` interface TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; } const buildTree = (preorder: number[], inorder: number[]): TreeNode | null => { if (preorder.length === 0) { return null; } const rootValue = preorder[0]; const root = { val: rootValue, left: null, right: null }; const rootIndex = inorder.indexOf(rootValue); const leftInorder = inorder.slice(0, rootIndex); const rightInorder = inorder.slice(rootIndex + 1); const leftPreorder = preorder.filter(val => leftInorder.includes(val)); const rightPreorder = preorder.filter(val => rightInorder.includes(val)); root.left = buildTree(leftPreorder, leftInorder); root.right = buildTree(rightPreorder, rightInorder); return root; }; ``` ### 回答2: 二叉树是一种常见的数据结构,可以使用前序遍历数组和中序遍历数组来构建一个二叉树。在typescript中,可以通过建立一个树节点类,并使用递归方法来完成这个过程。 首先,我们需要定义一个树节点类,它具有一个值属性和左右子节点属性: ```typescript class TreeNode { value: any; left: TreeNode | null; right: TreeNode | null; constructor(value:any) { this.value = value; this.left = null; this.right = null; } } ``` 接下来,我们可以定义一个函数来根据输入的前序遍历数组和中序遍历数组来生成二叉树: ```typescript function buildTree(preorder: any[], inorder: any[]): TreeNode | null { if(preorder.length === 0 || inorder.length === 0) { return null; } const rootValue = preorder[0]; const rootNode = new TreeNode(rootValue); const rootIndex = inorder.indexOf(rootValue); const leftPreorder = preorder.slice(1, rootIndex + 1); const rightPreorder = preorder.slice(rootIndex + 1); const leftInorder = inorder.slice(0, rootIndex); const rightInorder = inorder.slice(rootIndex + 1); rootNode.left = buildTree(leftPreorder, leftInorder); rootNode.right = buildTree(rightPreorder, rightInorder); return rootNode; } ``` 在上面的代码中,我们首先判断前序遍历数组和中序遍历数组是否为空,如果为空则返回null。接着,我们取前序遍历数组的第一个元素作为根节点的值,并创建一个根节点。然后,我们使用中序遍历数组找到根节点在其中的位置,将其分为左子树和右子树的中序遍历数组。再根据左子树和右子树的中序遍历数组的长度,分别获取前序遍历数组中对应的左子树和右子树的元素数组。最后,我们使用递归方法分别构建左子树和右子树,并将其赋值给根节点的左右子节点属性。最后返回根节点。 通过以上的步骤,我们就可以根据输入的前序遍历数组和中序遍历数组生成一个二叉树。 ### 回答3: 要通过输入的前序数组和中序数组来生成二叉树,可以按照以下步骤进行操作。 首先,检查输入的前序数组和中序数组是否有效。如果数组为空或数组长度不一致,则无法生成二叉树,返回空。 然后,从前序数组中取出第一个元素作为根节点,并创建一个新的二叉树节点。 接下来,根据根节点在中序数组中的位置,将中序数组分为左子树的中序数组和右子树的中序数组。 然后,根据左子树的中序数组的长度,在前序数组中取出对应数量的元素作为左子树的前序数组。 同样地,在前序数组中取出剩余的元素作为右子树的前序数组。 接着,使用递归的方式,分别将左子树的前序数组和中序数组作为输入,生成左子树,并将其设置为根节点的左子节点。 同样地,将右子树的前序数组和中序数组作为输入,生成右子树,并将其设置为根节点的右子节点。 最后,将生成二叉树返回。 以下是具体实现的typescript代码: ``` class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } function buildTree(preorder: number[], inorder: number[]): TreeNode | null { if (preorder.length === 0 || inorder.length === 0 || preorder.length !== inorder.length) { return null; } const rootVal = preorder[0]; const root = new TreeNode(rootVal); const rootIndex = inorder.indexOf(rootVal); const leftInorder = inorder.slice(0, rootIndex); const rightInorder = inorder.slice(rootIndex + 1); const leftPreorder = preorder.slice(1, 1 + leftInorder.length); const rightPreorder = preorder.slice(1 + leftInorder.length); root.left = buildTree(leftPreorder, leftInorder); root.right = buildTree(rightPreorder, rightInorder); return root; } ``` 以上是使用typescript实现根据输入的前序数组和中序数组生成二叉树的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值