Two fork tree and face test _ two fork Tree

Source: Internet
Author: User
Tags static class

Each node of a binary tree has a maximum of two Shang trees (no node with a degree greater than 2), and the subtree of the binary tree has a left and right part, and the order cannot be reversed. The first layer of the binary tree has at most 2^{i-1} nodes, and the two-forked tree with a depth of K has at most 2^k-1 nodes; for any binary tree T, if its terminal node number is N_0, and the number of degrees 2 is n_2, then n_0=n_2+1.

A tree with a depth of K, and a 2^k-1 node called full two fork tree; The depth is k, there are n nodes of the two-fork tree, when and only if each of its nodes with a depth of K two-tree, the number of nodes corresponding to 1 to N, called the complete binary tree.

1 full two fork tree and complete binary tree

A full two-fork tree must be a completely binary tree, but the reverse is not necessarily. The definition of two fork tree is in addition to the leaf node, the other node left and right children have, the depth of K two fork tree, Knot Point is 2 of the K-th square minus 1. The complete binary tree is the number from 1 to n one by one in a two-forked tree with a depth of k at each node.

2 Depth of the tree

The maximum level of the tree is the depth, such as the above, the depth is 3. It is easy to conclude that a tree with a depth of K has a maximum node number of 2 k times minus 1.

3 children of trees, brothers, parents

Above, B,c is the child of a, B,c is brothers, and A is B,c's parents. Written/interview Common 2 questions: 1, write three ways to traverse the code; 2, give the results of the first order, the middle order, give the following results; (test whether you really understand three sorts of sort)

public class TestClass {private static int[] Array = {1, 2, 3, 4, 5, 6, 7};  
	
   private static list<node> nodelist = null;  private static class Node {node leftchild; Zoozi Tree Node rightchild;  right subtree int data;  
           Nodes data node (int newdata) {leftchild = null;  
           Rightchild = null;  
       data = NewData;  
       The public static void Createbintree () {nodelist = new linkedlist<node> ();   
       for (int nodeindex = 0; Nodeindex < array.length; nodeindex++) {Nodelist.add (new Node (Array[nodeindex)); for (int parentindex = 0; Parentindex < ARRAY.LENGTH/2-1; parentindex++) {Nodeli  
           St.get (parentindex). Leftchild = NodeList. Get (Parentindex * 2 + 1);  
       Nodelist.get (parentindex). Rightchild = NodeList. Get (Parentindex * 2 + 2);  
int lastparentindex = ARRAY.LENGTH/2-1;       Nodelist.get (lastparentindex). Leftchild = NodeList. Get (Lastparentindex * 2 + 1); if (array.length% 2 = 1) {nodelist.get (lastparentindex). Rightchild = NodeList. Get (LA  
       Stparentindex * 2 + 2);   
           }/** * First-order traversal/public static void Preordertraverse (node node) {if (node = null)  
       Return  
       System.out.print (Node.data + "");  
       Preordertraverse (Node.leftchild);  
   Preordertraverse (Node.rightchild);  
           /** * Sequence traversal/public static void Inordertraverse (node node) {if (node = null)  
       Return  
       Inordertraverse (Node.leftchild);  
       System.out.print (Node.data + "");  
   Inordertraverse (Node.rightchild);  
           }/** * Subsequent traversal/public static void Postordertraverse (node node) {if node = null)  
       Return  
Postordertraverse (Node.leftchild);       Postordertraverse (Node.rightchild);  
   System.out.print (Node.data + ""); 
        public static void Main (string[] args) {//TODO auto-generated Method Stub createbintree ();  
        The value at the No. 0 index in the nodelist is the root node, node root = nodelist.get (0);
        System.out.print ("First Order:");
        Preordertraverse (root);
        System.out.println ("");
        System.out.print ("middle order:");
        Inordertraverse (root);
        System.out.println ("");
        System.out.print ("After:");
        
	Postordertraverse (root); }

}
By hierarchy, the principle is to use FIFO queues for implementation.
Hierarchy traverse   
 private static void Leveltraverse (node node) {
        if (node = = null) {return
            ;
        }
        
        linkedlist<node> queue = new linkedlist<> ();
        Node current = null;
        Queue.offer (node);
        
        while (!queue.isempty ()) {Current
            = Queue.poll ();
            
            System.out.println (Current.data + ""); Access Node
            
            if (current.leftchild!= null) {
                queue.offer (current.leftchild)
            }
            
            if (current.rightchild != null) {
                queue.offer (current.rightchild);
            }
        }
    }
Slightly modified by the level of code traversal, but also to achieve the maximum binary tree depth, minimum depth, each layer node width/maximum value, etc.
Public list<integer> largestvalues (TreeNode root) {
        list<integer> result = new arraylist<> ();
        linkedlist<treenode> ll = new linkedlist<> ();
        Ll.add (root);
        In the queue is each layer of nodes, loop through the current layer of all nodes, and then in the queue is the lower nodes
        //The algorithm also calculates the lowest depth of the binary tree, the outer circulation depth of 1, internal cycle to determine the left and right node is empty when the loop can while
        (true) {
            int Len = Ll.size (); Number of nodes per layer
            if (len = = 0) {break
                ;
            }

            int max = 0;
            while (Len > 0) {  //traverse all nodes of the current layer
                TreeNode node = Ll.poll ();  Moves the
                if (node.left!= null) {
                    ll.add (node.left) from the queue header;  Add to queue tail
                }
                if (node.right!= null) {
                    ll.add (node.right);
                }
                 if (node.val>max) max = Node.val;
                len--;
            }
            Result.add (max);
        }

        return result;
    }

Output results:
First Preface: 1 2 4 5 3 6 7
In order: 4 2 5 1 6 3 7
After: 4 5 2 6 7 3 1
The interview may tell you the first order and the results of the sequence, so that you draw a two fork tree and write the sequence of the order. Problem-solving skills: 1. The first order of first data is the root node, in the order of ordering the root node on the left side of the Zuozi, right is the right subtree.   That is, 1 is the root node, 4, 2, 5 is the left subtree, 6, 3, 7 is the right subtree, 2, the first order of the 2nd value is 2 is the root node left child node; The first value in the 3, 6, and 7 of the preceding sequence is the right child node of the root node. 3, remember that three sort is according to the root node reading time sequence definition, the first order is to first access the root node, in the order is the 2nd time to access the root node, after the 3rd time to access the root node.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.