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