Turn: Two recursive and non-recursive for fork trees

Source: Internet
Author: User

int deep  (root node) {     int s=0;     if (node)     {     s=deep (node->left) >deep (node->right)? Deep (Node->left) +1:deep (node->right) +1;     }     return s;}

The basic idea of recursion is: in order to obtain the depth of the tree, we can first seek the depth of the left and right subtree, take the larger of the two plus 1 is the depth of the tree, recursive return condition is if the node is empty, return 0

int Treedeep (Bintree BT) {2     int treedeep=0; 3     stack S; 4     stack tag; 5     Bintree p=bt; 6 while     (p!=null| | !isempty (S)) {7         while (p!=null) {8             push (S,P), 9             push (tag,0),             p=p->lchild;11         }12         if ( TOP (tag) ==1) {             deeptree=deeptree>s.length?deeptree:s.length;14             pop (S), N-             pop (tag),             p =null;17         }else{18             p=top (S),             p=p->rchild;20             Pop (tag),             push (tag,1),         }23     }24     return deeptree;25}

  

The basic idea of non-recursive realization:

Inspired by the subsequent traversal of the binary tree thought, we thought that we could use the method of subsequent traversal to find the depth of the binary tree, replace the size of the become stack s at each output place, and the maximum stack s length after the traversal is the depth of the stack.

The algorithm performs the following steps:

(1) When the tree is not empty, the pointer p points to the root node and p is the current node pointer.

(2) Press p into the stack s, 0 presses into the stack tag, and P executes its left child.

(3) Repeat step (2) until P is empty.

(4) If the stack top element in the tag stack is 1, skip to step (6). Return from right subtree

(5) If the stack top element in the tag stack is 0, skip to step (7). Return from Zuozi

(6) Compare the depth of the treedeep with the stack, take a larger assignment to the Treedeep, stack s and stack tag out of the stack operation, p points to null, and jumps to step (8).

(7) Point p to the stack s top element of the right child, pop stack tag, and put 1 into the stack tag. (Another way, directly modify the stack tag stack at the top of the value of 1 can also)

(8) Loop (2) ~ (7) until the stack is empty and p is empty

(9) Return to Treedeep, end traversal

Turn: Two recursive and non-recursive for fork trees

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.