Rebuild the binary tree and the next node of the binary tree

1. Rebuild Binary Tree

1.1 Knowing the preorder and middle order

/**
 * @author bro
 * @date 2020/9/21 19:30
 * @desc
 */
public class Rebuild {
    
    

    public static TreeNode rebuildBinaryTree(int[] preOrder, int[] inOrder) {
    
    
        if (preOrder == null || inOrder == null) return null;
        TreeNode root = rebuildBinaryTreeCore(preOrder, 0, preOrder.length - 1,
                inOrder, 0, inOrder.length - 1);
        return root;
    }

    public static TreeNode rebuildBinaryTreeCore(int[] preOrder, int startPreOrder, int endPreOrder,
                                                 int[] inOrder, int startInOrder, int endInOrder) {
    
    
        if (startInOrder > endInOrder || startPreOrder > endPreOrder) {
    
    
            return null;  //停止递归的条件
        }
        TreeNode root = new TreeNode(preOrder[startPreOrder]);
        for (int i = startInOrder; i < endInOrder; i++) {
    
    
            if (preOrder[startInOrder] == inOrder[i]) {
    
    
                //左子树
                root.left = rebuildBinaryTreeCore(preOrder, startPreOrder + 1, (i - startInOrder) + startPreOrder,
                        inOrder, startInOrder, i - 1);
                //右子树
                root.right = rebuildBinaryTreeCore(preOrder, (i - startInOrder) + startPreOrder + 1, endPreOrder,
                        inOrder, i + 1, endInOrder);
            }
        }
        return root;
    }

    public static void main(String[] args) {
    
    
        int preorder[] = {
    
    1, 2, 4, 7, 3, 5, 6, 8};
        int inorder[] = {
    
    4, 7, 2, 1, 5, 3, 8, 6};
        TreeNode treeNode = rebuildBinaryTree(preorder, inorder);
        System.out.println(treeNode.toString());
    }
}

2.2 Knowing the middle order and post order

/**
 * @author bro
 * @date 2020/9/21 19:58
 * @desc 有中序和后序重建二叉树代码:
 */
public class Rebuild2 {
    
    
    public static TreeNode rebuildBinaryTree(int[] postOrder, int[] inOrder) {
    
    
        if (postOrder == null || inOrder == null) return null;
        TreeNode root = rebuildBinaryTreeCore(postOrder, 0, postOrder.length - 1,
                inOrder, 0, inOrder.length - 1);
        return root;
    }

    public static TreeNode rebuildBinaryTreeCore(int[] postOrder, int startPostOrder, int endPostOrder,
                                                 int[] inOrder, int startInOrder, int endInOrder) {
    
    
        if (startPostOrder > endPostOrder || startInOrder > endInOrder) return null; //终止递归
        TreeNode root = new TreeNode(postOrder[endPostOrder]);
        for (int i = startInOrder; i <= endInOrder; i++) {
    
    
            if (postOrder[endPostOrder] == inOrder[i]) {
    
    
                //左子树
                root.left = rebuildBinaryTreeCore(postOrder, startInOrder, startPostOrder - 1 + (i - startInOrder),
                        inOrder, startInOrder, i - 1);
                //右子树
               root.right =  rebuildBinaryTreeCore(postOrder, startPostOrder + (i - startInOrder), endPostOrder - 1,
                        inOrder, i + 1, endInOrder);
            }
        }
        return root;
    }

    public static void main(String[] args) {
    
    
        int []inorder = {
    
    1, 2, 3, 4, 5, 6, 7};
        int []postorder = {
    
    2, 4, 3, 1, 6, 7, 5};
        TreeNode treeNode = rebuildBinaryTree(postorder, inorder);
        System.out.println(treeNode);
        System.out.println(treeNode.value);
        System.out.println(treeNode.left.value);
        System.out.println(treeNode.right.value);
    }
}

2. The next node of the binary tree

The three situations are:

  1. If the node has a right subtree, it is the leftmost node of the right subtree
  2. The node has no right subtree, the left node of the parent node, the next node is the parent node
  3. The node has no right subtree, the right node of the parent node, and the parent node traverses upward until it is the left node of its parent node.
/**
 * @author bro
 * @date 2020/9/21 21:16
 * @desc 二叉树的下一个节点
 * 1. 如果节点有右子树,就是右子树的最左节点
 * 2. 节点没有右子树,是父节点的左节点,下一个节点就是父节点
 * 3. 节点没有右子树,是父节点的右节点,父节点一直向上遍历,直到他是它父节点的左节点
 */
public class GetNext {
    
    
    public static BinaryTreeNode nextNode(BinaryTreeNode pNode) {
    
    
        if (pNode == null) return null;
        BinaryTreeNode pNext = null;
        if (pNode.right != null) {
    
      // 第一种情况
            BinaryTreeNode pRight = pNode.right;
            while (pRight.left != null) pRight = pRight.left;
            pNext = pRight;
        } else if (pNode.parent != null) {
    
    
            BinaryTreeNode pCurrent = pNode;
            BinaryTreeNode pParent = pNode.parent;
            while (pParent != null && pCurrent == pParent.right) {
    
     //第三种情况
                pCurrent = pParent;
                pParent = pCurrent.parent;
            }
            pNext = pParent;  //第二种
        }
        return pNext;
    }

    public static void main(String[] args) {
    
    
        BinaryTreeNode a = new BinaryTreeNode('a');
        BinaryTreeNode b = new BinaryTreeNode('b');
        BinaryTreeNode c = new BinaryTreeNode('c');
        BinaryTreeNode d = new BinaryTreeNode('d');
        BinaryTreeNode e = new BinaryTreeNode('e');
        BinaryTreeNode f = new BinaryTreeNode('f');
        BinaryTreeNode g = new BinaryTreeNode('g');
        BinaryTreeNode h = new BinaryTreeNode('h');
        BinaryTreeNode i = new BinaryTreeNode('i');
        a.left = b;
        a.right = c;
        a.parent = null;
        b.left = d;
        b.right = e;
        b.parent = a;
        c.left = f;
        c.right = g;
        c.parent = a;
        d.left = null;
        d.right = null;
        d.parent = b;
        e.left = h;
        e.right = i;
        e.parent = b;
        f.left = null;
        f.right = null;
        f.parent = c;
        g.left = null;
        g.right = null;
        g.parent = c;
        h.left = null;
        h.right = null;
        h.parent = e;
        i.left = null;
        i.right = null;
        i.parent = e;

        System.out.println(nextNode(a) != null ? nextNode(a).value : "null");
        System.out.println(nextNode(b) != null ? nextNode(b).value : "null");
        System.out.println(nextNode(c) != null ? nextNode(c).value : "null");
        System.out.println(nextNode(d) != null ? nextNode(d).value : "null");
        System.out.println(nextNode(e) != null ? nextNode(e).value : "null");
        System.out.println(nextNode(f) != null ? nextNode(f).value : "null");
        System.out.println(nextNode(g) != null ? nextNode(g).value : "null");
        System.out.println(nextNode(h) != null ? nextNode(h).value : "null");
        System.out.println(nextNode(i) != null ? nextNode(i).value : "null");
    }
}

class BinaryTreeNode {
    
    
    char value;
    BinaryTreeNode left;
    BinaryTreeNode right;
    BinaryTreeNode parent;
    public BinaryTreeNode(char value) {
    
    
        this.value = value;
    }
}

Guess you like

Origin blog.csdn.net/Zmd_23/article/details/108740620