Sicily:计算二叉查找树的高度

Description

给定一个二叉查找树,要求计算其高度,每个二叉查找树将给出先序与中序的遍历。

例如:一个二叉查找树其先序遍历为:16, 10, 4, 15, 23 ; 中序遍历为 4, 10, 15, 16, 23,则其高度为2(假定空树高度为-1,只有根节点的数高度为0)

Input

The first line is the number of test cases. For each test case, the first line is the preorder of keys, the second  line is the inorder of the keys.

第一行输入测试用例个数。

对于每个测试用例,第一行是key值的先序遍历,第二行是key值的中序遍历

Output

对于每个测试用例,用一行输出树的高度

Sample Input

 Copy sample input to clipboard

2
4 5 6
4 5 6
6 4 8 9 10
4 6 8 9 10

Sample Output

2
3
#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

struct TreeNode{
	int data;
	TreeNode* left;
	TreeNode* right;
	TreeNode(char data):data(data),left(NULL),right(NULL){
	}
};

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
   if(preorder.empty()) return NULL;
   TreeNode* root = new TreeNode(preorder[0]);
   int pos = 0;
   while(inorder[pos] != preorder[0]) pos++;
   vector<int> left_pre(preorder.begin()+1, preorder.begin()+pos+1);
   vector<int> right_pre(preorder.begin()+pos+1, preorder.end());
   vector<int> left_in(inorder.begin(), inorder.begin()+pos);
   vector<int> right_in(inorder.begin()+pos+1,inorder.end());
   root->left = buildTree(left_pre, left_in);
   root->right = buildTree(right_pre, right_in);
   return root;
}

int h(TreeNode* root) {
	if(!root) return -1;
	return max(h(root->left), h(root->right)) + 1;
}

void extract(vector<int>& v, string& s) {
	stringstream ss(s);
	int data;
	while(ss >> data) v.push_back(data);
}

int main() {
   int numTest;
   cin >> numTest;
   cin.get();
   while(numTest--) {
	 vector<int> preorder;
	 vector<int> inorder;
   	 string pre, in;
   	 getline(cin, pre);
   	 getline(cin, in);
   	 extract(preorder,pre);
   	 extract(inorder,in);
	 TreeNode* root = NULL;
	 root = buildTree(preorder, inorder);
	 cout << h(root) << endl;
   }
   return 0;
}

  

时间: 2024-08-07 04:31:50

Sicily:计算二叉查找树的高度的相关文章

Android 计算view 的高度

上午在做一个QuickAction里嵌套一个ListView,在Demo运行没事,结果引入到我的项目里,发现我先让它在Button上面,结果是无视那个Button的高度,这很明显,就是那个Button的高度计算不正确. 看了下别人的建议,大概分为三类: 参数设置: ? mRootView.measure(0, 0); 在draw之前回调: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ViewTreeObserver vto =view.getViewTreeObs

iOS开发动态计算cell的高度

在iOS开发过程中,我们经常会用到UITableView, 谈到UITableView当然少不了UITableViewCell.那么有时候我们就会有疑惑,怎么样才能让cell的高度根据文字的大小多少,以及照片的高度来动态设计呢? 下面我们来看一下,到底怎么做才能让cell的高度动态变化,让界面看起来更美观协调一些呢? //动态设置cell的高度 + (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo { //1.图片的高度 //让图片等比例

(7)计算二叉树的高度和结点数——3

通过使用后序遍历的方式计算二叉树的高度.可以先计算左子树的高度h1,后计算右子树的高度h2,树的高度h3. h3 = max (h1, h2) + 1 ; 二叉树的高度,也就是从根结点出发一直到叶结点的路径的长度. 因为在每种遍历方法中对每个结点都仅访问一次,所以可以在进行遍历时对结点的数目进行计算. --整理自<C/C++程序员面试宝典>

用Model来计算cell的高度

效果: 将计算cell高度的方法直接移植到Model当中,初始化的瞬间就计算好了高度,非常好用! 源码: Model // // Model.h // // Copyright (c) 2014年 Y.X. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface Model : NSObject @property (nonatomic, strong)

iOS不得姐项目--精华模块上拉下拉的注意事项,日期显示,重构子控制器,计算cell的高度(只计算一次),图片帖子的显示

一.上拉下拉注意事项 使用MJRefresh中的上拉控件自动设置透明 当请求下页数据通过page的时候,注意的是上拉加载更多数据失败的问题,下拉加载数据失败了,页数应该还原.或者是请求成功的时候再将页数修改 二.帖子中的日期显示问题(操作日期的两个类的使用) 期望达到的效果:如图 <1>NSDate -- 需要通过NSDateFormatter(日期格式类)将日期转换成相同的格式,才能相互运算,计算出来的时间间隔是以秒数来呈现的. <2>NSCalendar(日历类) -- 通过当

iOS开发总结-UITableView 自定义cell和动态计算cell的高度

UITableView cell自定义头文件: shopCell.h #import <UIKit/UIKit.h> @interface shopCell : UITableViewCell @property (strong, nonatomic)  UIImageView *image;@property (strong, nonatomic)  UILabel *name;@property (strong, nonatomic)  UILabel *itemshop;@propert

简单计算字符串的高度

计算字符串的高度有很多种,这里写下最常用的简单计算字符串的高度 // // NSString+NSStringExt.h // UIFontSize // // Created by mac on 15/11/14. // Copyright (c) 2015年 叶炯. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface NSString (NSSt

计算文本的高度

计算文本的高度分两种情况,指定文本只有1行和多行,可以写方法返回字符串的size,options通常使用NSStringDrawingUsesLineFragmentOrigin,这样整个文本将以单行文本的矩形来计算整个文本高度 ①文字显示一行 -(CGSize)sizeOneLineText:(NSString *)text font:(UIFont *)font{ CGSize textSize = [text boundingRectWithSize:CGSizeMake(CGFLOAT_

兔子--计算listview的高度,解决listview与scrollview控件冲突

/** * 计算ListView的高度 * * @param listView */ public void setListViewHeightBasedOnChildren(ListView listView) { // 获取ListView对应的Adapter OrderGoodsAdapter listAdapter = (OrderGoodsAdapter) listView.getAdapter(); if (listAdapter == null) { return; } int t