Java版本:
- 二叉树的构建
/**
* 根据链表数据构建LinkedList<Integer> linkedList =
new LinkedList(Arrays.asList(new Integer[]{3,2,9,null,null,10,null,null,8,null,4}));
*/
private TreeNode creatTree(LinkedList<Integer> linkedList) {
TreeNode treeNode = null;
if (null == linkedList || linkedList.isEmpty()) {
return null;
}
Integer value = linkedList.removeFirst();
if (null != value) {
treeNode = new TreeNode(value);
treeNode.leftTree = creatTree(linkedList);
treeNode.rightTree = creatTree(linkedList);
}
return treeNode;
}
/**
* 根据先序,中序构建二叉树
* @param pre 先序数组
* @param in 中序数组
* @return
*/
public TreeNode reConstructBinaryTree2(int[] pre, int[] in) {
if (pre == null || in == null || pre.length == 0 || in.length == 0) {
return null;
}
if (pre.length != in.length) {
return null;
}
TreeNode root = new TreeNode(pre[0]);
for (int i = 0; i < pre.length; i++) {
if (pre[0] == in[i]) {
root.leftTree = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i + 1), Arrays.copyOfRange(in, 0, i));
root.rightTree = reConstructBinaryTree(Arrays.copyOfRange(pre, i + 1, pre.length), Arrays.copyOfRange(in, i + 1, in.length));
}
}
return root;
}
/**
* 根据中序,后续构建二叉树
* @param in 中序数组
* @param pos 后续数组
* @return
*/
public static TreeNode reConstructBinaryTree(int[] in, int[] pos) {
if(in == null || pos == null || in.length == 0 || pos.length == 0) {
return null;
}
if (in.length != pos.length){
return null;
}
TreeNode root = new TreeNode(pos[pos.length - 1]);
for (int i = 0; i < in.length; i++) {
if (pos[pos.length - 1] == in[i]) {
root.leftTree = reConstructBinaryTree(Arrays.copyOfRange(in, 0 , i), Arrays.copyOfRange(pos, 0, i));
root.rightTree = reConstructBinaryTree(Arrays.copyOfRange(in, i+1, in.length), Arrays.copyOfRange(pos, i, pos.length - 1));
}
}
return root;
}
- 二叉树的遍历
/**
* 先序遍历
* 根、左、右
*/
private void preOrderTraveral(TreeNode treeNode) {
if (null == treeNode) {
return;
}
System.out.println(treeNode.value);
preOrderTraveral(treeNode.leftTree);
preOrderTraveral(treeNode.rightTree);
}
/**
* 中序遍历
* 左、根、右
*/
private void inOrderTraveral(TreeNode treeNode) {
if (null == treeNode) {
return;
}
preOrderTraveral(treeNode.leftTree);
System.out.println(treeNode.value);
preOrderTraveral(treeNode.rightTree);
}
/**
* 后续遍历
* 左、右、根
*/
private void postOrderTraveral(TreeNode treeNode) {
if (null == treeNode) {
return;
}
preOrderTraveral(treeNode.leftTree);
preOrderTraveral(treeNode.rightTree);
System.out.println(treeNode.value);
}
/**
* 二叉树非递归前序遍历
* @param treeNode
*/
private void preOrderTraveralWithStack(TreeNode treeNode) {
Stack<TreeNode> stack = new Stack<>();
if (null == treeNode) {
return;
}
TreeNode node = treeNode;
while (treeNode != null || !stack.isEmpty()) {
}
}
Kotlin版本:
fun main() {
val preArray = arrayOf(1, 2, 4, 7, 3, 5, 6, 8)
val inArray = arrayOf(4, 7, 2, 1, 5, 3, 8, 6)
val posArray = arrayOf(7, 4, 2, 5, 8, 6, 3, 1)
println("二叉树中序、后续构造:")
println("中序:" + inArray.contentToString())
println("后续:" + posArray.contentToString())
val root = reConstructBinaryTree(inArray, posArray)
println("二叉树先序、中序构造:")
println("先续:" + preArray.contentToString())
println("中序:" + inArray.contentToString())
val root2 = reConstructBinaryTree2(preArray, inArray)
println("二叉树先序遍历:")
binaryTreePreorderTraversal(root)
println()
println("二叉树中序遍历:")
binaryTreeInorderTraversal(root)
println()
println("二叉树后序遍历:")
binaryTreePostorderTraversal(root)
}
/**
* 根据中序,后续构建二叉树
* @param inArray 中序数组
* @param posArray 后续数组
* @return
*/
fun reConstructBinaryTree(inArray: Array<Int>?, posArray: Array<Int>?): TreeNode<Int>? {
if (inArray == null || posArray == null || inArray.isEmpty() || posArray.isEmpty()) return null
if (inArray.size != posArray.size) return null
var root: TreeNode<Int> = TreeNode(posArray[posArray.size - 1])
for (i in inArray.indices) {
val index = posArray.size - 1
if (posArray[index] == inArray[i]) {
root.leftTree = reConstructBinaryTree(Arrays.copyOfRange(inArray, 0, i), Arrays.copyOfRange(posArray, 0, i))
root.rightTree = reConstructBinaryTree(Arrays.copyOfRange(inArray, i + 1, inArray.size), Arrays.copyOfRange(posArray, i, posArray.size - 1))
}
}
return root
}
/**
* 根据先序,中续构建二叉树
* @param inArray 中序数组
* @param posArray 后续数组
* @return
*/
fun reConstructBinaryTree2(preArray: Array<Int>?, inArray: Array<Int>?): TreeNode<Int>? {
if (preArray == null || inArray == null || preArray.isEmpty() || inArray.isEmpty()) return null
if (preArray.size != inArray.size) return null
var root: TreeNode<Int> = TreeNode(preArray[0])
for (i in inArray.indices) {
if (preArray[0] == inArray[i]) {
root.leftTree = reConstructBinaryTree2(Arrays.copyOfRange(preArray, 1, i + 1), Arrays.copyOfRange(inArray, 0, i))
root.rightTree = reConstructBinaryTree2(Arrays.copyOfRange(preArray, i+1, preArray.size), Arrays.copyOfRange(inArray, i+1, inArray.size))
}
}
return root
}
/**
* 先序遍历
*/
fun binaryTreePreorderTraversal(root: TreeNode<Int>?) {
if(root == null) {
return
}
print("" + root.value + ",")
binaryTreePreorderTraversal(root.leftTree)
binaryTreePreorderTraversal(root.rightTree)
}
/**
* 中序遍历
*/
fun binaryTreeInorderTraversal(root: TreeNode<Int>?) {
if(root == null) {
return
}
binaryTreeInorderTraversal(root.leftTree)
print("" + root.value + ",")
binaryTreeInorderTraversal(root.rightTree)
}
/**
* 后续遍历
*/
fun binaryTreePostorderTraversal(root: TreeNode<Int>?) {
if(root == null) {
return
}
binaryTreePostorderTraversal(root.leftTree)
binaryTreePostorderTraversal(root.rightTree)
print("" + root.value + ",")
}