Bottom View of a Binary Tree

Source: Internet
Author: User

Given a Binary Tree, we need to print the bottom view from left to right. A node x is there in output if x are the bottommost node at its horizontal distance. Horizontal distance of left child of a node x are equal to horizontal distance of x minus 1, and that's right child is Hor Izontal distance of x plus 1.

Examples:

                                          8/   \                    5      3                    /\    14

For the above tree the output should is 5, 10, 3, 14, 25.

If There is multiple bottom-most nodes for a horizontal distance from root, then print the later one on level traversal. For example, in the below diagram, 3 and 4 is both the Bottom-most nodes at horizontal distance 0, we need to print 4.

                                                             8/   \    /                 5      3 4                    /\                        10    

For the above tree the output should is 5, 10, 4, 14, 25.


Solution: Figure out the distance of the leftmost node of the two-fork tree, the distance from the rightmost node of the two-fork tree can be obtained, the distance range of all nodes of the binary tree is calculated, and if the horizontal distance of the root node is 9, the distance between the two binary trees on the top is [-2, 2]. In other words, there should be 5 output nodes. So how do you calculate the horizontal distance per node? The first step is to traverse the binary tree, according to the rules, the root node's left child's horizontal distance is the root node horizontal distance minus 1, the root node to the right child horizontal distance is the root node horizontal distance plus 1, the level of traversal of the binary tree process, even if the horizontal distance of each node, but requires the output of the horizontal distance only one So to leave the last node with the same horizontal distance value, map can do it.


#include <map> #include <vector> #include <iostream>using namespace std;typedef struct TreeNode {int Data;struct treenode* lchild;struct treenode* rchild;} TreeNode; treenode* createnode (int data) {treenode* node = (treenode*) malloc (sizeof (TreeNode)); if (NULL = = node) return null;node-& Gt;data = Data;node->lchild = Null;node->rchild = Null;return node;} int getleftmostdistance (treenode* root) {if (NULL = = root) return 0;int left = 0;if (root->lchild) {left = Getleftmostdi Stance (Root->lchild)-1;} return left;} int getrightmostdistance (treenode* root) {if (NULL = = root) return 0;int right = 0;if (root->rchild) {right = Getrightmo Stdistance (Root->rchild) + 1;} return right;} void Printmap (Map<int, treenode*> MP) {map<int, Treenode*>::iterator iter;iter = Mp.begin (); while (iter! = MP . End ()) {treenode* node = iter->second;cout << node->data << ""; iter + +;} cout << Endl;} void Getbottommostnode (treenode* root) {vector<int> diSvec;vector<treenode*> Vec;map<int, treenode*> mp;vec.push_back (Root);d isvec.push_back (0); Mp.insert (  Map<int, Treenode*>::value_type (0, root)), int curpos = 0;while (CurPos < Vec.size ()) {int size = Vec.size (); while (CurPos < size) {int dist = Disvec[curpos]; treenode* node = vec[curpos];map<int, Treenode*>::iterator iter;if (node->lchild) {Vec.push_back (node->  Lchild);d Isvec.push_back (dist-1), iter = Mp.find (dist-1), if (iter! = Mp.end ()) {mp.erase (ITER);} mp.insert (Map<int, Treenode*>::value_type (Dist-1, Node->lchild));} if (node->rchild) {vec.push_back (node->rchild);d isvec.push_back (dist + 1); iter = Mp.find (dist + 1); if (iter! = MP.E nd ()) {mp.erase (ITER);} mp.insert (Map<int, Treenode*>::value_type (dist + 1, node->rchild));} curpos++;}} cout << Endl;printmap (MP);} treenode* Createtree () {treenode* root = createnode; root->lchild = CreateNode (8); root->rchild = CreateNode (22 ); root->lchild->lchild = CreateNode (5); root->lchild->rchild = CreateNode (3); root->rchild->lchild = CreateNode (4);root->rchild-> Rchild = CreateNode (+); root->lchild->rchild->lchild = CreateNode (Ten);root->lchild->rchild-> Rchild = CreateNode (+); return root;} int main (int argc, char* argv[]) {treenode* root = Createtree (); Getbottommostnode (root); return 0;}


Bottom View of a Binary Tree

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.