Solve Tree Problems Recursively
"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的更多相关文章
- 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 ...
- [Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming
Every dynamic programming algorithm starts with a grid. It entails solving subproblems and builds up ...
- [原]Water Water Union-Find Set & Min-Spanning Tree Problems' Set~Orz【updating...】
[HDU] 1213 - How Many Tables [基础并查集,求父节点个数] 1856 -More is better [基础并查集,注意内存,HDU数据水了,不用离散化,注意路径压缩的方式 ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
随机推荐
- ecshop 国付宝支付接口
function get_code($order, $payment){ $version = '2.2'; $charset = '1'; $language = '1'; $signType = ...
- linux中vi编辑器(转载)
三种模式相互切换 在命令终端输入vi进入vi编辑器. 命令模式:进入编辑器即进入命令模式, 输入模式:在命令模式下输入“i ”进入输入模式: 末行模式:按“:”进入末行模式: 在输入模式切换至末行模式 ...
- 模式匹配之尺度空间---scale space
转载:http://www.cnblogs.com/cfantaisie/archive/2011/06/14/2080917.html 主要步骤 1).尺度空间的生成: 2).检测尺度 ...
- RS485总线典型电路介绍
一.RS485总线介绍: RS485总线是一种常见的串行总线标准,采用平衡发送与差分接收的方式,因此具有抑制共模干扰的能力.在一些要求通信距离为几十米到上千米的时候,RS485总线是一种应用最为广泛的 ...
- ElasticSearch(八)关于document的一些知识点
先查看一条数据: GET /ecommerce/product/5 { "_index" : "ecommerce", "_type" : ...
- Android笔记之自定义对话框
效果如下图 对话框布局 dialog_uninstallation_confirmation.xml <?xml version="1.0" encoding="u ...
- virtualBox 不能开启一个新任务的错误
2016.06.05 这两天想在virtualbox上安装CentOS7.0玩,遇到一个问题: 不能为虚拟电脑 CentOS7 打开一个新任务. The virtual machine 'CentOS ...
- NSArray / NSSet / NSDictory 三者的异同点
NSArray / NSSet / NSDictory 三者的异同点 NSArray 是一个有序对象的一个集合.相当于一个队列存储,可以有重复的数进去. NSSet 比较典型的一个HASH表(集合)算 ...
- Python序列——字符串
字符串 1 string模块预定义字符串 2 普通字符串与Unicode字符串 3 只适用于字符串的操作 4 原始字符串 5 Unicode字符串操作符 内建函数 1 标准类型函数与序列操作函数 2 ...
- Gym - 100187E E - Two Labyrinths —— bfs
题目链接:http://codeforces.com/gym/100187/problem/E 题解:一开始做的时候是将两幅图合并,然后直接bfs看是否能到达终点.但这种做法的错的,因为走出来的路对于 ...