二叉树的垂直和_打印二叉树的垂直和

二叉树的垂直和

Problem statement: Given a binary tree, find the vertical sum for the binary tree along each vertical line.

问题陈述:给定一棵二叉树,沿着每条垂直线找到二叉树的垂直和。

Solution:

解:

First we need to understand what vertical sum is. Let go through an example to understand what vertical sum is.

首先,我们需要了解什么是垂直和。 让我们通过一个例子来了解什么是垂直和。

Print vertical sum of a binary tree

For the above tree, let's check what the vertical sum is for the tree.

对于上面的树,让我们检查一下树的垂直和。

Print vertical sum of a binary tree 2

Just consider, we have partitioned the tree nodes into column & rows where rows are the level no starting from 0. Then the above tree can be converted into the above table easily.

考虑一下,我们将树节点划分为列和行,其中行是从0开始的级别。然后,可以将上面的树轻松转换为上面的表。

  1. Start from root. Root column is 0.

    从根开始。 根列为0。

  2. If next node is at left of current node

    如果下一个节点在当前节点的左侧

    Then column of next node=column of current node-1

    然后,下一个节点的列=当前节点-1的列

    Else

    其他

    Column of next node=column of current node+1

    下一个节点的列=当前节点的列+1

Using the above steps the tree can be easily partitioned to the table. It is to observe that there may be several entry at a specific (row, column) position. Like here, at Level3, column no 1 has two entry 4 & 11.

使用上述步骤,可以轻松地将树分区到表中。 可以观察到在特定(行,列)位置可能有多个条目。 像这里一样,在Level3,第1列有两个条目4和11。

Rest is about doing sums for each column and printing it.

剩下的就是为每一列求和并打印出来。

Thus the vertical sum output should be 2, 12, 8, 20, 9 (from col -2 to col 2 direction).

因此,垂直和输出应为2、12、8、20、9(从col -2到col 2方向)。

Though the visual description seems to be very easy to solve this problem, in programing view it’s not that easy.

尽管视觉描述似乎很容易解决此问题,但在编程视图中并不是那么容易。

求垂直和的算法 (Algorithm to find vertical sum)

The basic concept is to do pre-order traversal & while traversing we will keep track for each column (hashing)& will find cumulative sum.

基本概念是进行预遍历和遍历,我们将跟踪每列(散列)并找到累积和。

Thus the column is used as key & we need a map to process our algorithm.

因此,该列用作 ,我们需要一个映射来处理我们的算法。

  1. Initialize map<int, int>hash. //(ordered map)

    初始化map <int,int> hash 。 //(有序地图)

  2. Start from root. Column for root is 0.

    从根开始。 根的列为0。

    Call vertical_sum(root, 0, hash);

    调用vertical_sum(root,0,hash);

  3. Recursive function

    递归函数

  4. Function vertical_sum( tree node, column, reference of map hash)
    a) If (node== NULL)
            Return;
    //hash[column]+=node->data
    b) Add node value cumulatively for the column; 
    // vertical_sum(node->left, column-1, reference of map hash)
    c) Recursive do for the left subtree; 
    We are passing col-1 since it’s on the left
    // vertical_sum(node->right, column+1, reference of map hash)
    d) Recursive do for the right subtree; 
    We are passing col+1 since it’s on the right
    
    End Functionvertical_sum;
    
    
  5. Print the hash map to output the vertical sums for corresponding column.

    打印哈希图以输出对应列的垂直和。

C ++实现打印二叉树的垂直和 (C++ implementation to print vertical sum of a binary tree)

#include <bits/stdc++.h>
using namespace std;

// tree node is defined
class tree{
	public:
		int data;
		tree *left;
		tree *right;
};

//finding vertical sum
void findVerticalSum(tree* root,int col,map<int,int> &hash){ 
	if(root==NULL) //base case
		return;
	//finding sum for respecting column, hashing the column
	hash[col]+=root->data; 
	//recursively process left sub-tree
	findVerticalSum(root->left,col-1,hash); 
	//recursively process right sub-tree
	findVerticalSum(root->right,col+1,hash); 
}

void vertical_sum(tree* root){
	//ordered hash map
	map<int,int> hash; 
	//column no for root is 0
	findVerticalSum(root,0,hash); 
	cout<<"column"<<"\t"<<"sum\n";
	//printing the values from hash map
	for(auto it=hash.begin();it!=hash.end();it++){ 
		//it->first= column no(key) , 
		//it->second=vertical sum of respective column(value)
		cout<<it->first<<"\t"<<it->second<<endl;  
	}
}

// creating new node
tree* newnode(int data)  
{ 
	tree* node = (tree*)malloc(sizeof(tree)); 
	node->data = data; 
	node->left = NULL; 
	node->right = NULL; 
	return(node); 
} 

int main() 
{ 
	//**same tree is builted as shown in example**
	int c,K;
	cout<<"Tree is built like the example aforesaid"<<endl;
	//building the tree like as in the example
	tree *root=newnode(2); 
	root->left= newnode(7); 
	root->right= newnode(5); 
	root->right->right=newnode(9);
	root->right->right->left=newnode(4);
	root->left->left=newnode(2); 
	root->left->right=newnode(6);
	root->left->right->left=newnode(5);
	root->left->right->right=newnode(11);

	cout<<"finding vertical sums......"<<endl; 
	vertical_sum(root);

	return 0; 
} 

Output

输出量

Tree is built like the example aforesaid
finding vertical sums......
column  sum
-2      2
-1      12
0       8
1       20
2       9


翻译自: https://www.includehelp.com/icp/print-vertical-sum-of-a-binary-tree.aspx

二叉树的垂直和

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值