In this problem, we should consider the path to the bottom, instead of hit-and-run. As a result, the recursive starts summation from the children up to their parents not from up to down.
For example:
The given sums up value is 8:
then these are all correct paths:
8;
8=-2=2;
2=0=6;
2=0=6=-1=1;
Here is the full code. Very well design.
//http://leetcode.com/2010/09/how-to-pretty-print-binary-tree.html
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; struct BinaryTree { BinaryTree *parent, *left, *right; int data; BinaryTree(int val) : parent(NULL), left(NULL), right(NULL), data(val) { } }; class BTree{ private: BinaryTree *m_root; public: BTree(): m_root(NULL){} BTree(BinaryTree *t): m_root(t){} BinaryTree* getRoot() const{return m_root;} BinaryTree* balanceTree(BinaryTree* pnode, BinaryTree *parent, int *arr, int low, int high){ if(low>high || arr == NULL) return NULL; int mid = (low+high)>>1; pnode = new BinaryTree(arr[mid]); pnode->parent = parent; pnode->left = balanceTree(pnode->left, pnode, arr, low, mid-1); pnode->right = balanceTree(pnode->right, pnode, arr, mid+1, high); return pnode; } void PrintPre(BinaryTree *pnode) { if(pnode == NULL) return; cout << pnode->data << " "; PrintPre(pnode->left); PrintPre(pnode->right); } // Find the maximum height of the binary tree int maxHeight(BinaryTree *p) { if (!p) return 0; int leftHeight = maxHeight(p->left); int rightHeight = maxHeight(p->right); return (leftHeight > rightHeight) ? leftHeight + 1: rightHeight + 1; } // Convert an integer value to string string intToString(int val) { ostringstream ss; ss << val; return ss.str(); } // Print the arm branches (eg, / \ ) on a line void printBranches(int branchLen, int nodeSpaceLen, int startLen, int nodesInThisLevel, const deque
& nodesQueue, ostream& out) { deque
::const_iterator iter = nodesQueue.begin(); for (int i = 0; i < nodesInThisLevel / 2; i++) { out << ((i == 0) ? setw(startLen-1) : setw(nodeSpaceLen-2)) << "" << ((*iter++) ? "/" : " "); out << setw(2*branchLen+2) << "" << ((*iter++) ? "\\" : " "); } out << endl; } // Print the branches and node (eg, ___10___ ) void printNodes(int branchLen, int nodeSpaceLen, int startLen, int nodesInThisLevel, const deque
& nodesQueue, ostream& out) { deque
::const_iterator iter = nodesQueue.begin(); for (int i = 0; i < nodesInThisLevel; i++, iter++) { out << ((i == 0) ? setw(startLen) : setw(nodeSpaceLen)) << "" << ((*iter && (*iter)->left) ? setfill('_') : setfill(' ')); out << setw(branchLen+2) << ((*iter) ? intToString((*iter)->data) : ""); out << ((*iter && (*iter)->right) ? setfill('_') : setfill(' ')) << setw(branchLen) << "" << setfill(' '); } out << endl; } // Print the leaves only (just for the bottom row) void printLeaves(int indentSpace, int level, int nodesInThisLevel, const deque
& nodesQueue, ostream& out) { deque
::const_iterator iter = nodesQueue.begin(); for (int i = 0; i < nodesInThisLevel; i++, iter++) { out << ((i == 0) ? setw(indentSpace+2) : setw(2*level+2)) << ((*iter) ? intToString((*iter)->data) : ""); } out << endl; } // Pretty formatting of a binary tree to the output stream // @ param // level Control how wide you want the tree to sparse (eg, level 1 has the minimum space between nodes, while level 2 has a larger space between nodes) // indentSpace Change this to add some indent space to the left (eg, indentSpace of 0 means the lowest level of the left node will stick to the left margin) void printPretty(BinaryTree *root, int level, int indentSpace, ostream& out) { int h = maxHeight(root); int nodesInThisLevel = 1; int branchLen = 2*((int)pow(2.0,h)-1) - (3-level)*(int)pow(2.0,h-1); // eq of the length of branch for each node of each level int nodeSpaceLen = 2 + (level+1)*(int)pow(2.0,h); // distance between left neighbor node's right arm and right neighbor node's left arm int startLen = branchLen + (3-level) + indentSpace; // starting space to the first node to print of each level (for the left most node of each level only) deque
nodesQueue; nodesQueue.push_back(root); for (int r = 1; r < h; r++) { printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); branchLen = branchLen/2 - 1; nodeSpaceLen = nodeSpaceLen/2 + 1; startLen = branchLen + (3-level) + indentSpace; printNodes(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); for (int i = 0; i < nodesInThisLevel; i++) { BinaryTree *currNode = nodesQueue.front(); nodesQueue.pop_front(); if (currNode) { nodesQueue.push_back(currNode->left); nodesQueue.push_back(currNode->right); } else { nodesQueue.push_back(NULL); nodesQueue.push_back(NULL); } } nodesInThisLevel *= 2; } printBranches(branchLen, nodeSpaceLen, startLen, nodesInThisLevel, nodesQueue, out); printLeaves(indentSpace, level, nodesInThisLevel, nodesQueue, out); } void printREV(BinaryTree* head, int level){ vector
v; for(int i=0; i
data); head = head->parent; } while(!v.empty()){ cout<
<<" "; v.pop_back(); } cout<
data; if(tmp == sum) printREV(head, i); no = no->parent; } find_sum(head->left, sum); find_sum(head->right, sum); } void print2(vector
v, int level){ for(int i=level; i
<<"----------"<
<
balanceTree(bt2->getRoot(), NULL, a2, low2, high2); bt2 = new BTree(proot2); bt2->PrintPre(bt2->getRoot()); cout << "Tree pretty print with level=1 and indentSpace=0\n\n"; // Output to console bt2->printPretty(proot2, 1, 0, cout); // int sum = 8; cout << "\nall the paths sums to: "<< sum<
find_sum(proot2, sum); cout << "if Struct BinaryTree doesn't have parent pointer.........\n"; vector
v_additional; bt2->find_sum2(proot2, sum, v_additional, 0); return 0; } /* Executing the program.... $demo a2: 0----------13 5 8 4 3 -2 0 2 2 -3 3 4 0 1 6 Tree pretty print with level=1 and indentSpace=0 _______5______ / \ ___8__ ___2__ / \ / \ 4 -2 -3 _0 \ / \ / \ / \ 3 0 2 3 4 1 6 all the paths sums to: 8 if Struct BinaryTree has parent pointer......... 8 8 -2 2 5 2 -3 4 5 2 0 1 2 0 6 if Struct BinaryTree doesn't have parent pointer......... 8 8 -2 2 5 2 -3 4 5 2 0 1 2 0 6 */