把一个有序整数数组放到二叉树中
来源:互联网 发布:淘宝网老北京布鞋 编辑:程序博客网 时间:2024/05/22 06:13
因为数组是有序,数组的中间节点就是根节点,这样数组就可以分为2个部分,左子树和右子树
分析:本题考察二叉搜索树的建树方法,简单的递归结构。关于树的算法设计一定要联想到递归,因为树本身就是递归的定义。而,学会把递归改称非递归也是一种必要的技术。毕竟,递归会造成栈溢出,关于系统底层的程序中不到非不得以最好不要用。但是对某些数学问题,就一定要学会用递归去解决。
/** * Created by renshuang on 10/14/13. */public class TestConverTree { public static void main(String args[]){ int[] array={1,2,3,4,5}; TestConverTree tree=new TestConverTree(); tree.printMid(tree.arrayConvertBst(array,0,array.length-1)); } public void printMid(Node node){ if(node.left!=null){ printMid(node.left); } System.out.println(node.data); if(node.right!=null){ printMid(node.right); } } public Node arrayConvertBst(int[] data,int start,int end){ if(start>end){ return null; } int mid=(start+end)/2; Node current=new Node(data[mid]); current.left=arrayConvertBst(data,start,mid-1); current.right=arrayConvertBst(data,mid+1,end); return current; } class Node { int data; Node left; Node right; public Node(){ } public Node(int data){ this.data=data; } public Node(int data,Node left,Node right){ this.data=data; this.left=left; this.right=right; } }}
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 题目:把一个有序整数数组放到二叉树。
- 把一个有序整数数组放到二叉树
- 把一个有序整数数组放到二叉树。
- 把一个有序整数数组放到二叉树
- 把一个有序整数数组放到二叉树
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- 怎样编写一个程序,把一个有序整数数组放到二叉树中(按行输出)
- 编写一个程序,把一个有序整数数组放到二叉树中。
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 5、怎样编写一个程序,把一个有序整数数组放到二叉树中
- 编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- armeabi和armeabi-v7a
- SQL 语法解释器jsqlparser
- activex控件的学习
- android:layout_weight属性详解
- __attribute__用法小结
- 把一个有序整数数组放到二叉树中
- 一个时间编程简单例子
- Block介绍(二)内存管理与其他特性
- android view setVisibility():
- Android.mk文件语法规范及使用模板
- ODBC和ADO
- 解决<li>标签中<a>和<span>标签对不齐问题
- java synchronized详解
- Android 抓包