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 ...
随机推荐
- IOS版App的控件元素定位
前言 Android版App的控件元素可以通过Android studio自带的工具uiautomatorviewer来协助定位! IOS版App的控件元素可以通过Appium来实现(未实现),或ap ...
- java 多线程2(转载)
http://www.cnblogs.com/DreamSea/archive/2012/01/11/JavaThread.html Ø线程的概述(Introduction) 线程是一个程序的多个执行 ...
- WPF触发器(Trigger、DataTrigger、EventTrigger)
WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改外观属性,或者执行动画等操作. WPFtrigger的主要类型有:Trigger. ...
- XML解析PULL
解析xml是很经常使用的操作,除了SAX和DOM两种最经常使用的解析xml外,Pull解析器解析XML文件. 在Android的源代码中大量的使用Pull解析.pull不仅更加的面相对象,并且使用速度 ...
- stretchableImageWithLeftCapWidth
本文转载至 http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html (NSInteger)topCapHeight 这个函数是UIIm ...
- 2016/07/07 mymps(蚂蚁分类信息/地方门户系统)
mymps(蚂蚁分类信息/地方门户系统)是一款基于php mysql的建站系统.为在各种服务器上架设分类信息以及地方门户网站提供完美的解决方案. mymps,整站生成静态,拥有世界一流的用户体验,卓越 ...
- [Phoenix] 七、如何使用自增ID
摘要: 在传统关系型数据库中设计主键时,自增ID经常被使用.不仅能够保证主键的唯一,同时也能简化业务层实现.Phoenix怎么使用自增ID,是我们这篇文章的重点. 在传统关系型数据库中设计主键时,自增 ...
- 关于JavaScript的事件触发
突然知道JavaScript底层是怎么实现事件触发的,找到一个博客,功力不够,看的很迷糊,记载这里吧,后面再研究. [探讨]javascript事件机制底层实现原理
- vue路由的两种模式,hash与history
对于Vue 这类渐进式前端开发框架,为了构建SPA(单页面应用),需要引入前端路由系统,这也就是Vue-router存在的意义.前端路由的核心,就在于——— 改变视图的同时不会向后端发出请求. 一.为 ...
- javascript中获取class
js中没有获取class的办法,找了一些封装好的方法,这里整理一下 (1)先进行封装 //封装getClass function getClass(tagName,className) //获得标签名 ...