2014-05-12 06:27

题目链接

原题:

  1. 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 = ): val(_val), left(nullptr), right(nullptr) {};
  13. };
  14.  
  15. int height(TreeNode *root)
  16. {
  17. return root ? + max(height(root->left), height(root->right)) : ;
  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 ;
  63. }

Careercup - Microsoft面试题 - 5672369481842688的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. Python对Excel操作详解

      Python对Excel操作详解 文档摘要: 本文档主要介绍如何通过python对office excel进行读写操作,使用了xlrd.xlwt和xlutils模块.另外还演示了如何通过Tcl   ...

  2. JS 中的string.lastIndexOf()

    一直转不过来一个弯,就是string.lastIndexOf(searchString,position)  当有position这个参数时,结果是什么 先看代码: var text = 'Missi ...

  3. Spark的基本概念及工作原理

    Spark作业的基本概念 -Application:用户自定义的Spark程序,用户提交后,Spark为App分配资源将程序转换并执行. -Driver Program:运行Application的m ...

  4. python_22_enumerate

    a=['a','d','f','g'] for i in enumerate(a): print(i) #enumerate 把列表的下表以元组的形式取出来 #结果 # (0, 'a') # (1, ...

  5. VC-基础-WebBrowser控件中弹出新网页窗口

    用webbrowser控件浏览网页时,常弹出新的网页窗口,若不做任何控制的话,会在默认浏览器(一般是IE)中打开,这样就在新的窗口打开了,原程序就很难控制了,且存在webbrowser控件和IE的se ...

  6. JQuery从服务器端取得数据绑定到dropdownlist(select)中

    http://blog.csdn.net/gaofang2009/article/details/5840783 http://www.cnblogs.com/Mac_Hui/archive/2010 ...

  7. 项目:Vue+node+后台管理项目小结

    序:本文主要分两块说:项目机制,具体用到的知识块. 1. 项目机制 项目的原型以vue-cli为原型,进行项目的初步构建.项目以node.js服务和webpack打包机制为依托,将.vue文件打包为浏 ...

  8. C++大数问题

    1.大数的加法 语法:add(char a[],char b[],char s[]); 参数: a[]:被加数,用字符串表示,位数不限 b[]:加数,用字符串表示,位数不限 s[]:结果,用字符串表示 ...

  9. STL MAP使用注意事项

    Hat’s Words A hat’s word is a word in the dictionary that is the concatenation of exactly two other ...

  10. 1412: [ZJOI2009]狼和羊的故事

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4017  Solved: 2037[Submit][Status][Discuss] Descript ...