Sword Point offer problem sets 1

Source: Internet
Author: User

1. Print binary tree

The program is very simple, but it made a small mistake, dead and alive can not find, write code to pay attention to AH

Here the subtree, to note is node->left, the result is written root->left

vector<int> Printfromtoptobottom (TreeNode *root) {Vector<int>Res; if(NULL = =root)returnRes; TreeNode*node; Deque<TreeNode*>tmp;    Tmp.push_back (root);  while(Tmp.size ()) {node=Tmp.front (); Res.push_back (Node-val);        Tmp.pop_front (); if(node->Left ) tmp.push_back (node-Left ); if(node->Right ) tmp.push_back (node-Right ); }    returnRes;}
View Code

2. Seeking 1+2+...+n

classa{ Public: A () {++n;sum+=N; }      Static intgetsum () {returnsum; }    Static voidReset () {n=0; sum=0; }    ~A () {}Private:            Static intN; Static intsum;}; inta::n=0;inta::sum=0; classSolution { Public:         intSum_solution (intN) {a::reset ();//necessary, the test assembly repeats its operationA* tmp=NewA[n]; Delete[] tmp; returna::getsum (); }};
View Code

3. Two Tree image

classSolution { Public:    voidMirror (TreeNode *proot) {                if(null==proot| | (null==proot->left&&null==proot->Right )) return;    Mirrorset (Proot); }        voidMirrorset (treenode*root) {                if(null==root| | (null==root->left&&null==root->Right )) return; TreeNode*tmp=root->Left ; Root->left=root->Right ; Root->right=tmp; if(root->Left ) mirrorset (Root-Left ); if(root->Right ) mirrorset (Root-Right ); }};
View Code

Sword Point offer problem sets 1

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.