Careercup - Microsoft面试题 - 5672369481842688
2014-05-12 06:27
原题:
- Find the max height of a binary tree.
题目:计算二叉树的最大高度。
解法:最大高度?高度不就是最深的叶子节点到根节点的路径长度吗?我就当是高度吧,递归解决。
代码:
- // http://www.careercup.com/question?id=5672369481842688
- #include <algorithm>
- #include <iostream>
- #include <sstream>
- #include <string>
- using namespace std;
- struct TreeNode {
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
- };
- int height(TreeNode *root)
- {
- return root ? + max(height(root->left), height(root->right)) : ;
- }
- void constructBinaryTree(TreeNode *&root)
- {
- static int val;
- static string str;
- static stringstream sio;
- if (cin >> str && str != "#") {
- sio << str;
- sio >> val;
- root = new TreeNode(val);
- constructBinaryTree(root->left);
- constructBinaryTree(root->right);
- } else {
- root = nullptr;
- }
- }
- void deleteTree(TreeNode *&root)
- {
- if (root == nullptr) {
- return;
- } else {
- deleteTree(root->left);
- deleteTree(root->right);
- delete root;
- root = nullptr;
- }
- }
- int main()
- {
- TreeNode *root;
- while (true) {
- constructBinaryTree(root);
- if (root == nullptr) {
- break;
- }
- cout << height(root) << endl;
- deleteTree(root);
- }
- return ;
- }
Careercup - Microsoft面试题 - 5672369481842688的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- Python对Excel操作详解
Python对Excel操作详解 文档摘要: 本文档主要介绍如何通过python对office excel进行读写操作,使用了xlrd.xlwt和xlutils模块.另外还演示了如何通过Tcl ...
- JS 中的string.lastIndexOf()
一直转不过来一个弯,就是string.lastIndexOf(searchString,position) 当有position这个参数时,结果是什么 先看代码: var text = 'Missi ...
- Spark的基本概念及工作原理
Spark作业的基本概念 -Application:用户自定义的Spark程序,用户提交后,Spark为App分配资源将程序转换并执行. -Driver Program:运行Application的m ...
- python_22_enumerate
a=['a','d','f','g'] for i in enumerate(a): print(i) #enumerate 把列表的下表以元组的形式取出来 #结果 # (0, 'a') # (1, ...
- VC-基础-WebBrowser控件中弹出新网页窗口
用webbrowser控件浏览网页时,常弹出新的网页窗口,若不做任何控制的话,会在默认浏览器(一般是IE)中打开,这样就在新的窗口打开了,原程序就很难控制了,且存在webbrowser控件和IE的se ...
- JQuery从服务器端取得数据绑定到dropdownlist(select)中
http://blog.csdn.net/gaofang2009/article/details/5840783 http://www.cnblogs.com/Mac_Hui/archive/2010 ...
- 项目:Vue+node+后台管理项目小结
序:本文主要分两块说:项目机制,具体用到的知识块. 1. 项目机制 项目的原型以vue-cli为原型,进行项目的初步构建.项目以node.js服务和webpack打包机制为依托,将.vue文件打包为浏 ...
- C++大数问题
1.大数的加法 语法:add(char a[],char b[],char s[]); 参数: a[]:被加数,用字符串表示,位数不限 b[]:加数,用字符串表示,位数不限 s[]:结果,用字符串表示 ...
- STL MAP使用注意事项
Hat’s Words A hat’s word is a word in the dictionary that is the concatenation of exactly two other ...
- 1412: [ZJOI2009]狼和羊的故事
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4017 Solved: 2037[Submit][Status][Discuss] Descript ...