Data Structure-Binary Tree-path from the root to each leaf node in the output tree (application of the tree traversal algorithm ).

Source: Internet
Author: User
Void allpath (bitree T, stack & S) // output the path from the root tree to all leaf nodes {If (t) {push (S, T-> data ); if (! T-> left &&! T-> right) // If the left pointer and right pointer are both empty, the node is the leaf node printstack (s); else {allpath (t-> left, s); allpath (t-> right, S);} POP (s );}}

If the left pointer of a node is null, the node is a leaf node. In fact, the right pointer is a brother node in the actual tree. It is empty, as shown in. Red indicates the actual tree, and blue indicates the binary linked list converted from the tree.

 

Void outpath (bitree T, stack & S) // output the path from the forest root to all leaf nodes {While (! T) {// the path from the root node to the leaf node to be output. In essence, the path from the root node to the left is appended to the leaf node. Push (S, T-> data); If (! T-> lchild) // printstack (s); else outpath (t-> lchild, S); POP (s); t = T-> rchild; // t always go to the right }}

 

 

 

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.