zoukankan      html  css  js  c++  java
  • Java数据结构——根据遍历结果构造二叉树

    一、已知前序、中序、后序遍历结果的其中两种,还原二叉树。


    ①已知前序遍历结果:1,2,4,5,3,6,7
    中序遍历结果:4,2,5,1,6,3,7
    还原二叉树后BFS出结果。

    TreeNode.java

    public class TreeNode {
    private TreeNode leftChild;
    private TreeNode rightChild;
    private Object data;
    
    public TreeNode getLeftChild() {
    return leftChild;
    }
    
    public void setLeftChild(TreeNode leftChild) {
    this.leftChild = leftChild;
    }
    
    public TreeNode getRightChild() {
    return rightChild;
    }
    
    public void setRightChild(TreeNode rightChild) {
    this.rightChild = rightChild;
    }
    
    public Object getData() {
    return data;
    }
    
    public void setData(Object data) {
    this.data = data;
    }
    
    public TreeNode(Object data) {
    super();
    this.data = data;
    }
    }

    CreateTree.java:

    import java.util.LinkedList;
    import java.util.Queue;
    
    public class CreateTree {
    public static TreeNode genenateTree(int[] pre, int[] in) {
    if (pre.length == 0 || in.length == 0) {
    return null;
    }
    TreeNode root = new TreeNode(pre[0]);
    int i = 0;
    while (in[i] != pre[0]) {
    i++;
    }
    int[] preLeftChild = new int[i];
    int[] preRightChild = new int[pre.length - 1 - i];
    int[] inLeftChild = new int[i];
    int[] inRightChild = new int[pre.length - 1 - i];
    for (int j = 0; j < in.length; j++) {
    if (j < i) {
    preLeftChild[j] = pre[j + 1];
    inLeftChild[j] = in[j];
    } else if (j > i) {
    preRightChild[j - i - 1] = pre[j];
    inRightChild[j - i - 1] = in[j];
    }
    }
    root.setLeftChild(genenateTree(preLeftChild, inLeftChild));
    root.setRightChild(genenateTree(preRightChild, inRightChild));
    return root;
    }
    
    public static void visited(TreeNode node) {
    System.out.print(node.getData() + " ");
    }
    
    public static void LevenOrder(TreeNode root) {
    if (root == null) {
    return;
    }
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(root);
    TreeNode temp = null;
    while (!queue.isEmpty()) {
    temp = queue.poll();
    visited(temp);
    if (temp.getLeftChild() != null) {
    queue.add(temp.getLeftChild());
    }
    if (temp.getRightChild() != null) {
    queue.add(temp.getRightChild());
    }
    }
    }
    
    public static void main(String[] args) {
    int[] pre = { 1, 2, 4, 5, 3, 6, 7 };
    int[] in = { 4, 2, 5, 1, 6, 3, 7 };
    LevenOrder(genenateTree(pre, in));
    }
    }
    

      

    ②已知前序遍历结果:1,2,4,5,3,6,7
    后序遍历结果:4,5,2,6,7,3,1
    这种情况不能确定唯一的二叉树(即根据前序、后序结果不能确定唯一二叉树)


    ③已知 中序遍历结果:4,2,5,1,6,3,7
    后序遍历结果:4,5,2,6,7,3,1
    还原二叉树后BFS出结果。

    //这里只写出核心代码,其他部分可以参考第一种情况
    public class CreateTree {
    public static TreeNode genenateTree(int[] in, int[] post) {
    if (post.length == 0 || in.length == 0) {
    return null;
    }
    TreeNode root = new TreeNode(post[post.length - 1]);
    int i = 0;
    while (in[i] != post[post.length - 1]) {
    i++;
    }
    int[] postLeftChild = new int[i];
    int[] postRightChild = new int[post.length - 1 - i];
    int[] inLeftChild = new int[i];
    int[] inRightChild = new int[post.length - 1 - i];
    for (int j = 0; j < in.length; j++) {
    if (j < i) {
    postLeftChild[j] = post[j];
    inLeftChild[j] = in[j];
    } else if (j > i) {
    postRightChild[j - i - 1] = post[j - 1];
    inRightChild[j - i - 1] = in[j];
    }
    }
    root.setLeftChild(genenateTree(inLeftChild, postLeftChild));
    root.setRightChild(genenateTree(inRightChild, postRightChild));
    return root;
    }
    

      


    二、如果已知的前序、中序、后序的结果中包含占位符#,此时,只需知道其中一种遍历结果就能还原二叉树,且结果是唯一的。

    ①已知前序遍历结果是 :"1", "2", "4", "#", "#", "5", "#", "#", "3", "6", "#", "#", "7", "#", "#",还原二叉树后BFS出结果。

    import java.util.LinkedList;
    import java.util.Queue;
    
    public class CreateTree {
    static int count = 0;
    
    public static TreeNode genenateTree(String[] data) {
    TreeNode root = null;
    if (count >= data.length || data[count++].equals("#")) {
    root = null;
    } else {
    root = new TreeNode(data[count - 1]);
    root.setLeftChild(genenateTree(data));
    root.setRightChild(genenateTree(data));
    }
    return root;
    }
    
    public static void visited(TreeNode node) {
    System.out.print(node.getData() + " ");
    }
    
    public static void LevenOrder(TreeNode root) {
    if (root == null) {
    return;
    }
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(root);
    TreeNode temp = null;
    while (!queue.isEmpty()) {
    temp = queue.poll();
    visited(temp);
    if (temp.getLeftChild() != null) {
    queue.add(temp.getLeftChild());
    }
    if (temp.getRightChild() != null) {
    queue.add(temp.getRightChild());
    }
    }
    }
    
    public static void main(String[] args) {
    String[] dataStr = { "1", "2", "4", "#", "#", "5", "#", "#", "3", "6", "#", "#", "7", "#", "#" };
    LevenOrder(genenateTree(dataStr));
    }
    }
  • 相关阅读:
    [PHP] laravel框架注意bootstrap/cache
    [git] git拉取远程分支代码
    Prometheus监控系统(4)pushgateway及自定义脚本
    k8s 学习
    pxc /Galera Cluster详解
    Percona Monitoring and Management
    etcd获取所有key
    kubernetes Pod驱逐迁移drain用法
    kubernetes etcd数据管理
    办公环境下k8s网络互通方案
  • 原文地址:https://www.cnblogs.com/ericz2j/p/10730110.html
Copyright © 2011-2022 走看看