Tree_Graph 有序数组转BST @CareerCup
2014-03-03 03:02
337 查看
原文:
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.
译文:
给定一个有序数组(递增),写程序构建一棵具有最小高度的二叉树。
思路:
因为是有序的数组,所以直接提取中数为root,然后递归建立其左子树和右子树
package Tree_Graph;
import CtCILibrary.TreeNode;
public class S4_3 {
// 递归把有序数组转为BST
public static TreeNode createMinBST(int[] A, int start, int end) {
if ( start > end ) {
return null;
}
int mid = (start + end) / 2;
TreeNode root = new TreeNode(A[mid]);
root.left = createMinBST(A, start, mid-1);
root.right = createMinBST(A, mid+1, end);
return root;
}
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// We needed this code for other files, so check out the code in the library
TreeNode root = TreeNode.createMinimalBST(array);
System.out.println("Root? " + root.data);
System.out.println("Created BST? " + root.isBST());
System.out.println("Height: " + root.height());
}
}
Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height.
译文:
给定一个有序数组(递增),写程序构建一棵具有最小高度的二叉树。
思路:
因为是有序的数组,所以直接提取中数为root,然后递归建立其左子树和右子树
package Tree_Graph;
import CtCILibrary.TreeNode;
public class S4_3 {
// 递归把有序数组转为BST
public static TreeNode createMinBST(int[] A, int start, int end) {
if ( start > end ) {
return null;
}
int mid = (start + end) / 2;
TreeNode root = new TreeNode(A[mid]);
root.left = createMinBST(A, start, mid-1);
root.right = createMinBST(A, mid+1, end);
return root;
}
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// We needed this code for other files, so check out the code in the library
TreeNode root = TreeNode.createMinimalBST(array);
System.out.println("Root? " + root.data);
System.out.println("Created BST? " + root.isBST());
System.out.println("Height: " + root.height());
}
}
相关文章推荐
- Tree_Graph Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点 @CareerCup
- Tree_Graph LCA(Lowest Common Ancestor) 最低公共节点 (BST或者一般树) @CareerCup
- Tree_Graph Validate Binary Search Tree 检测一个BST是否有效 @CareerCup
- Soring&Searching 合并两个有序数组 @CareerCup
- Convert Sorted Array to Binary Search Tree 把一个有序数组转换成BST @LeetCode
- Leetcode 108 Convert Sorted Array to Binary Search Tree 将一个有序数组变成BST
- Leetcode 108. Convert Sorted Array to Binary Search Tree 有序数组转化BST 解题报告
- Tree_Graph 打印所有和为定值的路径 @CareerCup
- [LeetCode]Convert Sorted Array to Binary Search Tree 将有序数组转换成BST
- Leetcode Convert Sorted Array to Binary Search Tree 有序数组转换成二叉搜索树BST
- Tree_Graph 判断是否平衡二叉树 @CareerCup
- Tree_Graph 有向图是否存在路径 @CareerCup
- LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)
- LeetCode OJ 之 Convert Sorted Array to Binary Search Tree(把有序数组转化为二叉搜索树)
- CareerCup Find the diameter of the tree
- LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解
- 108. Convert Sorted Array to Binary Search Tree | 有序数组生成平衡二叉搜索树
- Convert Sorted List to Binary Search Tree 把有序链表转为BST@LeetCode
- BST:有序数组构建成二叉搜索树
- CareerCup Given preorder traversal array of a BST, recontruct the BST.