Careercup - Microsoft面试题 - 5672369481842688
2014-05-12 06:27
题目链接
原题:
Find the max height of a binary tree.
题目:计算二叉树的最大高度。
解法:最大高度?高度不就是最深的叶子节点到根节点的路径长度吗?我就当是高度吧,递归解决。
代码:
1 // http://www.careercup.com/question?id=5672369481842688 2 #include <algorithm> 3 #include <iostream> 4 #include <sstream> 5 #include <string> 6 using namespace std; 7 8 struct TreeNode { 9 int val; 10 TreeNode *left; 11 TreeNode *right; 12 TreeNode(int _val = 0): val(_val), left(nullptr), right(nullptr) {}; 13 }; 14 15 int height(TreeNode *root) 16 { 17 return root ? 1 + max(height(root->left), height(root->right)) : 0; 18 } 19 20 void constructBinaryTree(TreeNode *&root) 21 { 22 static int val; 23 static string str; 24 static stringstream sio; 25 26 if (cin >> str && str != "#") { 27 sio << str; 28 sio >> val; 29 root = new TreeNode(val); 30 constructBinaryTree(root->left); 31 constructBinaryTree(root->right); 32 } else { 33 root = nullptr; 34 } 35 } 36 37 void deleteTree(TreeNode *&root) 38 { 39 if (root == nullptr) { 40 return; 41 } else { 42 deleteTree(root->left); 43 deleteTree(root->right); 44 delete root; 45 root = nullptr; 46 } 47 } 48 49 int main() 50 { 51 TreeNode *root; 52 53 while (true) { 54 constructBinaryTree(root); 55 if (root == nullptr) { 56 break; 57 } 58 cout << height(root) << endl; 59 deleteTree(root); 60 } 61 62 return 0; 63 }
转载于:https://www.cnblogs.com/zhuli19901106/p/3722685.html
Careercup - Microsoft面试题 - 5672369481842688相关推荐
- Careercup - Microsoft面试题 - 5428361417457664
2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...
- Careercup - Microsoft面试题 - 5752271719628800
2014-05-10 20:31 题目链接 原题: Given an array of integers and a length L, find a sub-array of length L su ...
- Careercup - Google面试题 - 4699414551592960
2014-05-06 13:34 题目链接 原题: we have a random list of people. each person knows his own height and the ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 4877486110277632
2014-05-08 05:16 题目链接 原题: Given a circle with N defined points and a point M outside the circle, fin ...
- python可以不用主函数吗_python自定义函数可以向前引用不用声明
Careercup - Microsoft面试题 - 6543214668414976 2014-05-11 02:56 题目链接 原题: Write a function called FooBar ...
- origin和python有什么不同_python爬虫之git的使用(origin说明)
1.首先我们回忆两个命令 #git remote add origin 远程仓库链接 #git push -u origin master 我们一起看看这个命令,git是git的一级命令,push就是 ...
- 计算机办公软件应用三套题目,办公软件应用试题中级汇编第3套
办公软件应用中级Microsoft Office2010试题汇编 办公软件应用中级Microsoft Office2010试题汇编 (第03套) 第1单元:操作系统应用 (1) 第2单元:文字录入与编 ...
最新文章
- 给GRUB添加新的项目
- opencv 运动检测
- Python 中函数的 收集参数 机制
- ThinkPHP5有关模型hasOne、hasMany、belongsTo详解
- HarmonyOS之将SVG文件转换为XML文件
- maven常用的命令(window控制台和linux命令是一致的)
- java的构造函数详解,Java构造函数与普通函数用法详解
- jQuery对象与dom对象的转换
- 双重差分模型能做固定效应吗_数据分析之道 | 双重差分法(DID)
- vue新增属性是否会响应式更新?
- hdu 4454 Stealing a Cake 三分法
- matlab 箱图不显示异常值_欧姆龙E5EZ温控器PV显示值跳动异常的处理,一看就懂...
- svn可视化操作工具
- 画图必备工具:25个常用Matplotlib图的Python代码总结
- IBEACON 解析代码(C语言和LUA语言)
- (七)设定目标:原理与方法
- openGL增强表面细节--凹凸贴图
- 关于最新版go-cqhttp无法登录qq
- 计算机会计数据处理流程是怎样的,实现会计电算化后,会计数据的处理流程依旧和手工..._高级会计师_帮考网...
- 高分子聚合物过滤器折叠滤芯