"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.

Solve Tree Problems Recursively的更多相关文章

  1. TED #09# You don't have to be an expert to solve big problems

    Tapiwa Chiwewe: You don't have to be an expert to solve big problems Collection noticed a haze hangi ...

  2. [Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming

    Every dynamic programming algorithm starts with a grid. It entails solving subproblems and builds up ...

  3. [原]Water Water Union-Find Set & Min-Spanning Tree Problems' Set~Orz【updating...】

    [HDU] 1213 - How Many Tables [基础并查集,求父节点个数] 1856 -More is better [基础并查集,注意内存,HDU数据水了,不用离散化,注意路径压缩的方式 ...

  4. [LeetCode] Symmetric Tree 判断对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  5. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  6. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  7. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  8. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  9. 【leetcode】Symmetric Tree

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

随机推荐

  1. iOS8 PUSH解决方法

    本文转载至 http://blog.csdn.net/pjk1129/article/details/39548523     - (void)registerForRemoteNotificatio ...

  2. windows 打开文件夹

    @echo off rem 建立链接 net use \\192.168.2.3\share /user:username password rem 打开共享文件夹 explorer \\192.16 ...

  3. Elasticsearch + Logstash + Kibana 搭建教程

    # ELK:Elasticsearch + Logstash + Kibana 搭建教程 Shipper:日志收集者.负责监控本地日志文件的变化,及时把日志文件的最新内容收集起来,输出到Redis暂存 ...

  4. 预料外的变量值的改变是很多bug的源头

  5. 使用JavaScript获取浏览器UserAgent

    可以在浏览器地址栏输入about:version来查看UserAgent等信息 但是在Win10系统,本人亲测,IE和Edge用这样的方式都获取不到信息 在我惯用的QQ浏览器上倒是可以获取到 为了能方 ...

  6. DuiLib笔记之Control常用属性

    name 指定控件名称,同一窗口内必须唯一,类型:STRING float 用于指定控件是否使用绝对定位,或设置FloatPercent,类型:BOOL,默认值为false,格式:float=&quo ...

  7. MARA 附加结构(增强字段)

  8. HLS切片机

    参考: 1,linux下搭建生成HLS所需的.ts和.m3u8文件http://www.cnblogs.com/mystory/archive/2013/04/07/3006200.html2,iPh ...

  9. Linux ARM交叉编译工具链制作过程【转】

    本文转载自:http://www.cnblogs.com/Charles-Zhang-Blog/archive/2013/02/21/2920999.html 一.下载源文件 源代码文件及其版本与下载 ...

  10. php函数的参数引用变量

    在php.ini中将allow_call_time_pass_reference的值改为'on'.