实现创建二叉树

创建二叉树

1. 前序遍历创建二叉树

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
class TreeNode {
    public TreeNode left;
    public char val;
    public TreeNode right;
    
    public TreeNode(char val) {
        this.val = val;
    }
}
public class Main {

    public static int i; 
    public static TreeNode createTree(String str) {
        TreeNode root = null;
        char ch = str.charAt(i);
        if (ch != '#') {
            root = new TreeNode(ch);
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        }else {
            i++;
        }
        return root;
    }

    public static void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case

           // 输入字符串
           String str = in.nextLine();

           // 创建二叉树
           TreeNode root = createTree(str);

           // 中序打印
           inOrder(root);
        }
    }

    
}

2. 前序与中序遍历构造二叉树

前序遍历判断根 中序遍历判断左右子树

设一颗二叉树的先序遍历和中序遍历如下 画出这棵树:先序遍历:E  F  H  I  G  J  K  中序遍历:H  F  I  E  J  K  G

题解:

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        this.postIndex = postorder.length-1;
        return buildTreeChild(inorder,postorder,0,inorder.length-1);
    }
    public int postIndex;
    public TreeNode buildTreeChild(int[] inorder, int[] postorder,int inBegin,int inEnd) {
        if (inBegin > inEnd) {
            return null;
        }
        TreeNode root = new TreeNode(postorder[postIndex--]);
        int rootIndex = findRootIndex(inorder,inBegin,inEnd,root.val);
        root.right = buildTreeChild(inorder,postorder,rootIndex+1,inEnd);
        root.left = buildTreeChild(inorder,postorder,inBegin,rootIndex-1);
        return root;
    }
    public int findRootIndex(int[] inorder,int inbegin,int inend,int key) {
        for (int i = inbegin;i <= inend;i++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }
}

 

3. 后序中序遍历构造二叉树

后序遍历判断根:

设一颗二叉树的后序遍历和中序遍历序列如下 画出这棵树:

后序遍历:b  d  e  c  a   中序遍历: b  a  d  c  e

题解:

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        this.postIndex = postorder.length-1;
        return buildTreeChild(inorder,postorder,0,inorder.length-1);
    }
    public int postIndex;
    public TreeNode buildTreeChild(int[] inorder, int[] postorder,int inBegin,int inEnd) {
        if (inBegin > inEnd) {
            return null;
        }
        TreeNode root = new TreeNode(postorder[postIndex--]);
        int rootIndex = findRootIndex(inorder,inBegin,inEnd,root.val);
        root.right = buildTreeChild(inorder,postorder,rootIndex+1,inEnd);
        root.left = buildTreeChild(inorder,postorder,inBegin,rootIndex-1);
        return root;
    }
    public int findRootIndex(int[] inorder,int inbegin,int inend,int key) {
        for (int i = inbegin;i <= inend;i++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }
}

 

posted @ 2024-01-21 08:17  qyx1  阅读(48)  评论(0)    收藏  举报