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 ... 
随机推荐
- 零基础学python-2.18 异常
			这一节说一下异常except 继续沿用上一节的代码.我有益把文件名称字搞错.然后在结尾部分加上异常捕捉: try: handler=open("12.txt")#在这里我特别将文件 ... 
- oracle指定访问某表或某视图
			在oracle中,想创建一个账号,然后只能只读地访问指定的表,怎么搞? 一.为特定的表创建视图 创建视图的时候还可以加上过滤条件,连访问哪些数据都可以指定. create or replace vie ... 
- (转)  实现wince datagrid 上下滑屏数据浏览
			开发 基于wince 手持设备数据库应用时 由于是触摸屏 当datagrid 数据过多 往往用户烦于去控制又窄又细的上下滚动条 尤其是高分辨率的屏上 (如魅族M8系统 720×480) 而且datag ... 
- Javascript的参数详解
			函数可以有参数也可以没有参数,如果定义了参数,在调用函数的时候没有传值,默认设置为undefined 在调用函数时如果传递参数超过了定义时参数,jS会忽略掉多余参数 jS中不能直接写默认值,可以通过a ... 
- [通信]Linux User层和Kernel层常用的通信方式
			转自:https://bbs.csdn.net/topics/390991551?page=1 netlink:https://blog.csdn.net/stone8761/article/deta ... 
- java 内部类(转)
			原文: http://www.cnblogs.com/nerxious/archive/2013/01/24/2875649.html 内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 ... 
- Mall电商项目总结(一)——项目概述
			项目概述 此电商项目为本人学习项目,后端 使用nginx实现负载均衡转发请求到多台tomcat服务器,使用多台 redis服务器分布式 缓存用户登录信息. 项目已经部署到阿里云服务器,从阿里云linu ... 
- 有待总结的KMP算法 sdut oj 2463 学密码学一定得学程序
			学密码学一定得学程序 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 曾经,ZYJ同学非常喜欢密码 学.有一天,他发现了一个很长很 ... 
- "未预编译文件 因此不能请求该文件"问题处理
			手里一个项目重新编译后发布,访问时提示未预编译文件“default.aspx”, 因此不能请求该文件.综合网上的解决方法,做了如下操作: 1.重新安装了AJAX Extension: 2.项目添加引用 ... 
- codeforces   B. Ilya and Queries  解题报告
			题目链接:http://codeforces.com/problemset/problem/313/B 题目意思:给出一个只有 "." 和 "#" 组成的序 ... 
