[geeksforgeeks] Bottom View of a Binary Tree
http://www.geeksforgeeks.org/bottom-view-binary-tree/
Bottom View of a Binary Tree
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可以做到。
http://blog.csdn.net/zzran/article/details/41981969
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <limits.h>
using namespace std; void printArray(int *array, int size)
{
for(int i = ; i < size; i++)
cout << array[i]<< "\t" ;
cout << endl;
} void printVector(vector<int> array )
{
for(int i = ; i <array.size(); i++)
cout << array[i]<< "\t" ;
cout << endl;
} struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; void preorder(TreeNode * root)
{
if(root == NULL) return;
cout << root->val << "\t" ;
preorder(root->left);
preorder(root->right);
} void inorder(TreeNode * root)
{
if(root == NULL) return;
inorder(root->left);
cout << root->val << "\t" ;
inorder(root->right);
} void postorder(TreeNode * root)
{
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
cout << root->val << "\t" ;
} struct newNode
{
TreeNode* m_node;
int m_idx;
newNode(TreeNode* node, int idx)
{
m_node = node;
m_idx = idx;
}
}; class Solution {
public:
vector<int> bottomView(TreeNode* root) {
queue<newNode* > q1;
queue<newNode* > q2;
vector<int> res;
map<int, int> mapping;// index -- value pair if(root != NULL)
{
q1.push(new newNode(root, ));
} int leftMost = ;
int rightMost = ;
while(!q1.empty())
{
newNode * p = q1.front();
q1.pop(); mapping[p->m_idx] = p->m_node->val; if(p->m_idx < leftMost)
leftMost = p->m_idx;
if(p->m_idx > rightMost)
rightMost = p->m_idx; if(p->m_node->left)
q2.push(new newNode(p->m_node->left, p->m_idx - ) );
if(p->m_node->right)
q2.push(new newNode(p->m_node->right, p->m_idx + )); if(q1.empty() /*&& !q2.empty()*/)
{
swap(q1, q2);
}
} for(map<int, int>::iterator it = mapping.begin(); it != mapping.end(); it++)
{
cout << it->first <<"\t" <<it->second << endl;
}
for(int i = leftMost ; i <= rightMost ; i++)
res.push_back(mapping[i]);
return res;
} }; int main()
{
TreeNode node0();
TreeNode node1();
TreeNode node2();
TreeNode node3();
TreeNode node4();
TreeNode node5();
TreeNode node6(); node0.left = &node1;
node0.right= &node2; node1.left = &node3;
node1.right= &node4; node2.left = &node5;
node2.right= &node6; Solution sl;
vector<int> res = sl.bottomView(&node0); printVector(res);
cout << endl;
return ;
}
另外,top view也可以用这样的方法,不是保留最后一个,而是保留第一次idx的结构,后续的数据不保存。
[geeksforgeeks] Bottom View of a Binary Tree的更多相关文章
- 【LeetCode】199. Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- 【刷题-LeetCode】199 Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- [geeksforgeeks] Convert a given Binary Tree to Doubly Linked List
http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Bin ...
- Print Nodes in Top View of Binary Tree
Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a ...
- Convert a given Binary Tree to Doubly Linked List
The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-li ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- [leetcode]199. Binary Tree Right Side View二叉树右侧视角
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
随机推荐
- PHP变量作用域以及地址引用问题
作用域的概念: 在PHP脚本的任何位置都可以声明变量,但是,声明变量的位置会大大影响访问变量的范围.这个可以访问的范围称为作用域. 主要的常用的包括:局部变量.全局变量.静态变量. 1.局部变量:就是 ...
- Java字符判断
从键盘上输入一个字符串,遍历该字符串中的每个字符,若该字符为小写字母,则输出“此字符是小写字母”:若为大写字母,则输出“此字符为大写字母”:否则输出“此字符不是字母”. 代码入下: import ja ...
- VS2012那点事儿
VS2012并不完美支持C99标准,这一点强烈的体现在如下的错误: 也就是是说你的变量定义必须在前面,一股脑儿全写完,然后才可以使用,如果你定义变量穿插在了其他地方,那么就会报上面的错误.略微有些遗憾 ...
- LinkedList存储一副扑克牌,实现洗牌功能。
package cd.itcast.runble; import java.util.LinkedList; import java.util.Random; /** * LinkedList存储一副 ...
- C# 多线程操作样例
using System; using System.Threading; //引用多线程 namespace ThreadTest { public class Alpha { public voi ...
- C# 常用的dialogresult reset 以及if else 等检查获取客户操作信息的操作方法
DialogResult reset; reset= MessageBox.Show("请检查您的输入信息是否按照规则输入的", "信息输入好像有问题哦", M ...
- 第十八章 数据访问(In .net4.5) 之 I/O操作
1. 概述 本章内容包括 文件操作.流操作.读写网络数据 以及 异步I/O操作. 2. 主要内容 2.1 文件操作 ① 使用 Drive 和 DriveInfo 访问磁盘信息. DriveInfo[] ...
- 第十一章 管理类型(In .net4.5) 之 管理对象的生命周期
1. 概述 本章内容包括 管理非托管资源.使用IDisposable接口 以及 管理析构器和垃圾回收. 2. 主要内容 2.1 理解垃圾回收机制 ① 代码执行的时候,内存中有两个地方存放数据项:堆 和 ...
- Moses创建一个翻译系统的基本过程记录,以后会按照每个过程详细说明,并给出每个步骤的参数说明
软件需求: 首先你必须要有Moses(废话哈哈).然后要有GIZA++用作词对齐(traning-model.perl的时候会用到).IRSTLM产生语言模型 大致步骤: 大体的步骤如下: 准备Par ...
- StyleCop学习笔记——自定义规则
本文将简单的一步一步的指导这可能有助于学习如何创建自己的规则 1.创建一个项目. Visual Studio创建一个新的类库项目.NET3.5 2.引用两个DLL,StyleCop.dll和Style ...