标签:des style blog ar io color os sp for
Given a Binary Tree, we need to print the bottom view from left to right. A node x is there in output if x is the bottommost node at its horizontal distance. Horizontal distance of left child of a node x is equal to horizontal distance of x minus 1, and that of right child is horizontal distance of x plus 1.
Examples:
20
/ 8 22
/ \ 5 3 25
/ \
10 14
For the above tree the output should be 5, 10, 3, 14, 25.
If there are multiple bottom-most nodes for a horizontal distance from root, then print the later one in level traversal. For example, in the below diagram, 3 and 4 are both the bottom-most nodes at horizontal distance 0, we need to print 4.
20
/ 8 22
/ \ / 5 3 4 25
/ \
10 14
For the above tree the output should be 5, 10, 4, 14, 25.
解决思路:算出二叉树最左边节点的距离,在算出二叉树最右边节点的距离,可以得出这棵二叉树所有节点的距离范围,如果根节点的水平距离为9,那么上边两个二叉树的距离范围是[-2, 2]。也就是说,输出节点应该有5个。那么怎么算每个节点的水平距离?首先要层次遍历二叉树,根据规则,根节点的左边孩子的水平距离是根节点水平距离减1,根节点右边孩子水平距离是根节点水平距离加1,层次遍历二叉树过程中,就算出了每个节点的水平距离,但是要求输出的水平距离只对应一个节点,所以要留下水平距离值相同的最后一个节点,用map可以做到。
#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->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 = getLeftMostDistance(root->lchild) - 1;
}
return left;
}
int getRightMostDistance(TreeNode* root) {
if (NULL == root)
return 0;
int right = 0;
if (root->rchild) {
right = getRightMostDistance(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);
disVec.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);
disVec.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);
disVec.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->rchild));
}
curPos++;
}
}
cout << endl;
printMap(mp);
}
TreeNode* createTree() {
TreeNode* root = createNode(20);
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(25);
root->lchild->rchild->lchild = createNode(10);
root->lchild->rchild->rchild = createNode(14);
return root;
}
int main(int argc, char* argv[]) {
TreeNode* root = createTree();
getBottommostNode(root);
return 0;
}标签:des style blog ar io color os sp for
原文地址:http://blog.csdn.net/zzran/article/details/41981969