Binary Tree path and

Source: Internet
Author: User


# Include "stdafx. H "# include <vector >#include <iostream> using namespace STD; # define a 16 // Binary Tree preordered traversal sequence int buffer [16] = {, 4, -1,-100,-1,-, 6,-1,-,-1,-1,-1 }; // Binary Tree struct typedef struct binary_tree_node {int data; struct binary_tree_node * ltree; struct binary_tree_node * rtree;} btnode; // create a new node btnode * create_node (void) {btnode * node; node = (btnode *) malloc (sizeof (btnode); Return node;} // create a binary tree based on the pre-ordered sequence/* clarify the problem: (1) When to perform binary Switch of tree branches ① When the left branch traverses to the leaf node ② when a new node is added to the right branch (2) when the node is added to the stack (3) */btnode * create_tree (int * BUF) {btnode * root; btnode * pnode, * temp; btnode * s [a]; bool ltree = true; int Index = 0; int m = 0; root = create_node (); root-> DATA = Buf [index ++]; s [M ++] = root; pnode = root; while (BUF [Index]! =-100) {If (ltree = true) {If (BUF [Index] =-1) {pnode-> ltree = NULL; index ++; ltree = false; pnode = s [-- m];} else {temp = create_node (); temp-> DATA = Buf [index ++]; pnode-> ltree = temp; s [M ++] = temp; pnode = temp ;}} else {If (BUF [Index] =-1) {pnode-> rtree = NULL; index ++; pnode = s [-- m];} else {temp = create_node (); temp-> DATA = Buf [index ++]; pnode-> rtree = temp; s [M ++] = temp; pnode = temp; ltree = true ;}}return root;} // recursively traverse void preorder_traversal (BT Node * pnode) {If (pnode! = NULL) {printf ("% d", pnode-> data);} else {return;} preorder_traversal (pnode-> ltree); preorder_traversal (pnode-> rtree ); return;} vector <btnode *> S; int sum = 0; int spec = 100; void printpath (void) {vector <btnode *>: iterator it = S. begin (); cout <"Path:"; while (it! = S. end () {cout <(* It)-> data <""; ++ it;} cout <Endl;} void postsum (btnode * node) {If (node-> ltree = NULL & node-> rtree = NULL) {sum + = node-> data; S. push_back (node); // If (sum = spec) {cout <sum <""; printpath () ;}if (! S. empty () {sum-= S. back ()-> data; S. pop_back ();} return;} else {sum + = node-> data; S. push_back (node); postsum (node-> ltree); postsum (node-> rtree);} If (! S. empty () {sum-= S. back ()-> data; S. pop_back () ;}return ;}int main (void) {btnode * root; root = create_tree (buffer); postsum (Root); System ("pause "); return 0 ;}

Binary Tree path and

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.