"Top-down" Solution

Here is the pseudocode for the recursion function maximum_depth(root, depth):

1. return if root is null
2. if root is a leaf node:
3. answer = max(answer, depth) // update the answer if needed
4. maximum_depth(root.left, depth + 1) // call the function recursively for left child
5. maximum_depth(root.right, depth + 1) // call the function recursively for right child

  

code:

int answer;		       // don't forget to initialize answer before call maximum_depth
void maximum_depth(TreeNode* root, int depth) {
if (!root) {
return;
}
if (!root->left && !root->right) {
answer = max(answer, depth);
}
maximum_depth(root->left, depth + 1);
maximum_depth(root->right, depth + 1);
}

  

"Bottom-up" Solution

1. return 0 if root is null                 // return 0 for null node
2. left_depth = maximum_depth(root.left)
3. right_depth = maximum_depth(root.right)
4. return max(left_depth, right_depth) + 1 // return depth of the subtree rooted at root

  

code:

int maximum_depth(TreeNode* root) {
if (!root) {
return 0; // return 0 for null node
}
int left_depth = maximum_depth(root->left);
int right_depth = maximum_depth(root->right);
return max(left_depth, right_depth) + 1; // return depth of the subtree rooted at root
}

  

Conclusion.

It is not easy to understand recursion and find out a recursion solution for the problem.

When you meet a tree problem, ask yourself two questions: can you determine some parameters to help the node know the answer of itself? Can you use these parameters and the value of the node itself to determine what should be the parameters parsing to its children? If the answers are both yes, try to solve this problem using a "top-down" recursion solution.

Or you can think the problem in this way: for a node in a tree, if you know the answer of its children, can you calculate the answer of the node? If the answer is yes, solving the problem recursively from bottom up might be a good way.

In the following sections, we provide several classic problems for you to help you understand tree structure and recursion better.

最新文章

  1. 如何区别char与varchar?
  2. JavaScript原型OOP——你上车了吗?
  3. tomcat监控
  4. 用SPCOMM 在 Delphi中实现串口通讯 转
  5. 简单使用Junit
  6. Blog CSS
  7. android NDK 实用学习(四)-类缓存
  8. 【转】ArrayList的toArray,也就是list.toArray[new String[list.size()]];,即List转为数组
  9. oracle 11gR2 RAC安装手册
  10. 浏览器history操作实现一些功能
  11. UIController子类控件 UI_06
  12. 如何再window下统计自己写的代码行
  13. 【转】Java并发编程:同步容器
  14. Applet程序组件与AJAX技术
  15. python threading模块2
  16. 深度学习在CTR预估中的应用
  17. Linux中wget用法
  18. C#中Serializable序列化
  19. sqlserver-一次updlock和withnolock和with check option 的报错原因分析
  20. OC 导入类 #import和@class 区别复习

热门文章

  1. java中的File文件读写操作
  2. MFC学习之Radio---MFC Radio按钮组的使用例子
  3. Apache/2.4.9启动错误:AH01630: client denied by server configuration
  4. 【上】安全HTTPS-全面具体解释对称加密,非对称加密,数字签名,数字证书和HTTPS
  5. 记一次FastJSON和Jackson解析json时遇到的中括号问题
  6. [自动化平台系列] - 初次使用 Macaca-前端自动化测试(1)
  7. MARA 附加结构(增强字段)
  8. LVS集群的负载调度
  9. POJ2018 Best Cow Fences —— 斜率优化DP
  10. IOC入门1