Binary Tree Reconstruction (sequence preorder traversal known and rear)

Reconstruction of a binary tree:

1. General topics will order traversal with the former set (order traversal or later), preorder, so you reconstruct binary output another traversal. If you preorder with no reconstruction can not be given a binary tree.

Here to learn simple known preamble, after seeking in order traversal sequence:

  First we have to know a few binary tree traversal features:

1. Front traversing Binary Tree: First, the root node traversal of a binary tree, then traverse left subtree, and then traverse the right subtree.

2. In traversing Binary Tree: First Traverse the left subtree, then the root node, and then traverse the right subtree.

3. After traversing Binary Tree: First Traverse the left subtree, then the right sub-tree, and finally the root node.

Is given by the: 1 with a predetermined sequence preorder traversal, the first data is a root node.

     2. given sequence preorder, preorder find previous root node is determined, then the sequence preorder according to this node into two parts, the first part of the root node, the left sub-tree of the entire binary tree, half It is part of the right subtree entire binary tree.

Code:

// 二叉树节点定义class TreeNode{
    TreeNode left;
    TreeNode right;
    int value ;
    public TreeNode(int value) {
        this.value = value;
    }
  public TreeNode(){
  }
}

 

// First filter invalid, determining whether a given sequence in a preamble sequence legal 

 public the TreeNode reBinaryTree (the TreeNode [] preOrder, the TreeNode [] inOrder) {
    // determines whether valid sequence length 
    
    int preLen = preOrder.length;
    int inLen = inOrder .length;
    IF (preLen == 0 || inLen == 0 ) {
        return  null ; 
    } 
    return ctBinaryTree (preOrder, inOrder, 0, 1,0-preLen,-inLen. 1 ); 
}

 

// The method then calls the recursive binary tree structure of binary tree structure 

public the TreeNode ctBinaryTree (the TreeNode [] preOrder, the TreeNode [] inOrder, int PRESTART, int preEnd, int inStart, int inEnd) {
      // sequence preorder traversal of a node according to the prior (root) to build a binary tree root. 
      Tree = the TreeNode new new Tree (preOrder [PRESTART]);
      // from preorder found in the root node, and then dividing the length of the left and right subtrees 
      int the root = 0 ;
      for (= the root inStart; the root <inEnd; the root ++ ) {
        IF (preOrder [ PRESTART] == inOrder [the root]) {
            BREAK ; 
          } 
      int leftLen = the root - inStart;
      int rightLen = inEnd - root;
      // 递归构造二叉树
      if(leftLen > 0){
         tree.left = ctBinartTree(preOrder , inOrder , preStart+1,preStart+leftLen,inStart,root-1);
        }
      if(rightLen > 0){
         tree.right = ctBinaryTree(preOrder , inOrder , preStart+1+leftLen,root+1,inEnd);        
        }
        return tree;
}

}

 













Guess you like

Origin www.cnblogs.com/songjinzhao/p/11440410.html