递归算法经典实例_掌握这10道经典面试算法题(含答案),攻克递归算法【程序员必备】...
- 1. 树的高度
- 2. 平衡树
- 3. 两节点的最长路径
- 4. 翻转树
- 5. 归并两棵树
- 6. 判断路径和是否等于一个数
- 7. 统计路径和等于一个数的路径数量
- 8. 子树
- 9. 树的对称
- 10. 最小路径
- 11. 统计左叶子节点的和
- 12. 相同节点值的最大路径长度
- 13. 间隔遍历
- 14. 找出二叉树中第二小的节点
欢迎关注笔者,优质文章都在这里等你。
递归
一棵树要么是空树,要么有两个指针,每个指针指向一棵树。树是一种递归结构,很多树的问题可以使用递归来处理。
1. 树的高度
104. Maximum Depth of Binary Tree (Easy)
public int maxDepth(TreeNode root) { if (root == null) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;}
2. 平衡树
110. Balanced Binary Tree (Easy)
3 / 9 20 / 15 7
平衡树左右子树高度差都小于等于 1
private boolean result = true;public boolean isBalanced(TreeNode root) { maxDepth(root); return result;}public int maxDepth(TreeNode root) { if (root == null) return 0; int l = maxDepth(root.left); int r = maxDepth(root.right); if (Math.abs(l - r) > 1) result = false; return 1 + Math.max(l, r);}
3. 两节点的最长路径
543. Diameter of Binary Tree (Easy)
Input: 1 / 2 3 / 4 5Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].private int max = 0;public int diameterOfBinaryTree(TreeNode root) { depth(root); return max;}private int depth(TreeNode root) { if (root == null) return 0; int leftDepth = depth(root.left); int rightDepth = depth(root.right); max = Math.max(max, leftDepth + rightDepth); return Math.max(leftDepth, rightDepth) + 1;}
4. 翻转树
226. Invert Binary Tree (Easy)
public TreeNode invertTree(TreeNode root) { if (root == null) return null; TreeNode left = root.left; // 后面的操作会改变 left 指针,因此先保存下来 root.left = invertTree(root.right); root.right = invertTree(left); return root;}
5. 归并两棵树
617. Merge Two Binary Trees (Easy)
Input: Tree 1 Tree 2 1 2 / / 3 2 1 3 / 5 4 7Output: 3 / 4 5 / 5 4 7public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if (t1 == null && t2 == null) return null; if (t1 == null) return t2; if (t2 == null) return t1; TreeNode root = new TreeNode(t1.val + t2.val); root.left = mergeTrees(t1.left, t2.left); root.right = mergeTrees(t1.right, t2.right); return root;}
6. 判断路径和是否等于一个数
Leetcdoe : 112. Path Sum (Easy)
Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / 7 2 1return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
路径和定义为从 root 到 leaf 的所有节点的和。
public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);}
7. 统计路径和等于一个数的路径数量
437. Path Sum III (Easy)
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / 5 -3 / 3 2 11 / 3 -2 1Return 3. The paths that sum to 8 are:1. 5 -> 32. 5 -> 2 -> 13. -3 -> 11
路径不一定以 root 开头,也不一定以 leaf 结尾,但是必须连续。
public int pathSum(TreeNode root, int sum) { if (root == null) return 0; int ret = pathSumStartWithRoot(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); return ret;}private int pathSumStartWithRoot(TreeNode root, int sum) { if (root == null) return 0; int ret = 0; if (root.val == sum) ret++; ret += pathSumStartWithRoot(root.left, sum - root.val) + pathSumStartWithRoot(root.right, sum - root.val); return ret;}
8. 子树
572. Subtree of Another Tree (Easy)
Given tree s: 3 / 4 5 / 1 2Given tree t: 4 / 1 2Return true, because t has the same structure and node values with a subtree of s.Given tree s: 3 / 4 5 / 1 2 / 0Given tree t: 4 / 1 2Return false.public boolean isSubtree(TreeNode s, TreeNode t) { if (s == null) return false; return isSubtreeWithRoot(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);}private boolean isSubtreeWithRoot(TreeNode s, TreeNode t) { if (t == null && s == null) return true; if (t == null || s == null) return false; if (t.val != s.val) return false; return isSubtreeWithRoot(s.left, t.left) && isSubtreeWithRoot(s.right, t.right);}
9. 树的对称
101. Symmetric Tree (Easy)
1 / 2 2 / / 3 4 4 3public boolean isSymmetric(TreeNode root) { if (root == null) return true; return isSymmetric(root.left, root.right);}private boolean isSymmetric(TreeNode t1, TreeNode t2) { if (t1 == null && t2 == null) return true; if (t1 == null || t2 == null) return false; if (t1.val != t2.val) return false; return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);}
10. 最小路径
111. Minimum Depth of Binary Tree (Easy)
树的根节点到叶子节点的最小路径长度
public int minDepth(TreeNode root) { if (root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); if (left == 0 || right == 0) return left + right + 1; return Math.min(left, right) + 1;}
11. 统计左叶子节点的和
404. Sum of Left Leaves (Easy)
3 / 9 20 / 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.public int sumOfLeftLeaves(TreeNode root) { if (root == null) return 0; if (isLeaf(root.left)) return root.left.val + sumOfLeftLeaves(root.right); return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);}private boolean isLeaf(TreeNode node){ if (node == null) return false; return node.left == null && node.right == null;}
12. 相同节点值的最大路径长度
687. Longest Univalue Path (Easy)
1 / 4 5 / 4 4 5Output : 2private int path = 0;public int longestUnivaluePath(TreeNode root) { dfs(root); return path;}private int dfs(TreeNode root){ if (root == null) return 0; int left = dfs(root.left); int right = dfs(root.right); int leftPath = root.left != null && root.left.val == root.val ? left + 1 : 0; int rightPath = root.right != null && root.right.val == root.val ? right + 1 : 0; path = Math.max(path, leftPath + rightPath); return Math.max(leftPath, rightPath);}
13. 间隔遍历
337. House Robber III (Medium)
3 / 2 3 3 1Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.public int rob(TreeNode root) { if (root == null) return 0; int val1 = root.val; if (root.left != null) val1 += rob(root.left.left) + rob(root.left.right); if (root.right != null) val1 += rob(root.right.left) + rob(root.right.right); int val2 = rob(root.left) + rob(root.right); return Math.max(val1, val2);}
14. 找出二叉树中第二小的节点
671. Second Minimum Node In a Binary Tree (Easy)
Input: 2 / 2 5 / 5 7Output: 5
一个节点要么具有 0 个或 2 个子节点,如果有子节点,那么根节点是最小的节点。
public int findSecondMinimumValue(TreeNode root) { if (root == null) return -1; if (root.left == null && root.right == null) return -1; int leftVal = root.left.val; int rightVal = root.right.val; if (leftVal == root.val) leftVal = findSecondMinimumValue(root.left); if (rightVal == root.val) rightVal = findSecondMinimumValue(root.right); if (leftVal != -1 && rightVal != -1) return Math.min(leftVal, rightVal); if (leftVal != -1) return leftVal; return rightVal;}
资料来源 : github 作者 : CyC2018
您的转发+关注就是对笔者最大的支持,欢迎关注。
对大厂架构设计,BAT面试题分享,编程语言理论或者互联网圈逸闻趣事这些感兴趣,欢迎关注笔者,没有错,干货文章都在这里。
递归算法经典实例_掌握这10道经典面试算法题(含答案),攻克递归算法【程序员必备】...相关推荐
- 大佬分享:180+道Java面试题目!含答案解析!
作者:我是offer 链接:https://www.nowcoder.com/discuss/84736 来源:牛客网 大厂常见问题 写视频点播网站文件下载接口 基础变量/数组写出模拟maven导入包 ...
- 2018互联网企业最新面试大纲180+道Java面试题目!含答案解析!
在进入正文之前,顺便给大家推荐一个Java架构方面的交流学习群:878249276,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发.高性能.分布式. ...
- 面试过阿里的P7大佬分享:180+道Java面试题目!含答案解析!
大厂常见问题 写视频点播网站文件下载接口 基础变量/数组写出模拟maven导入包过程 写出新变脸内存分配,模拟垃圾回收过程 50个白球50个红球,两个盒子,怎么放让人随机在一个盒子里抽到红球概率最高 ...
- 100道Python面试练习题【含答案】
Python是目前编程领域最受欢迎的语言.在本文中,我将总结Python面试中最常见的100个问题.每道题都提供参考答案,希望能够帮助你在2021年求职面试中脱颖而出,找到一份高薪工作.这些面试题涉及 ...
- 10个匿名浏览网页的隐私保护工具,程序员必备!
有很多的人喜欢浏览互联网,使用代理.有背后的原因是多方面的.最重要的原因之一,包括您的私人信息,不希望被泄露,我们程序员们经常访问被封锁的网站,这时候怎么办呢,用这个工具以后再也不用愁了 当您浏览互联 ...
- sql经典实例_读书笔记 前三章
sql经典实例_读书笔记 温故SQL以及数据库相关知识 1.检索记录 select * from emp //检索所有列 where dep = 10 //选择出指定行 or comm is not ...
- 厉害了,关于String的10道经典面试题
转载自 厉害了,关于String的10道经典面试题 1.String是基本数据类型吗? 2.String是可变的话? 3.怎么比较两个字符串的值一样,怎么比较两个字符串是否同一对象? 4.switch ...
- 后端开发10道经典面试题的剖析, 技术方向如何决定职业方向
10道经典面试题的剖析, 技术方向如何决定职业方向 1. time_wait, close_wait如何解决? 2. 服务器接入抖动如何解决? 3. malloc的实现原理 视频讲解如下,点击观看: ...
- 10 道大厂面试必考的计算机网络问题-陶辉 极客时间
大厂中更多会考察你的长板. 在大厂中要学会求助 1.TCP的三次握手机制,为什么要三次? 为什么需要握手? 需要同步序列号,当然也有MSS(最大报文段长度),滑动窗口. 为什么是3次? 正常想法应该是 ...
最新文章
- Spring+SpringMVC+MyBatis深入学习及搭建(十)——MyBatis逆向工程
- 笔记本上的小键盘计算机怎样用,笔记本小键盘怎么开,详细教您笔记本小键盘怎么开启...
- 一维信号小波阈值去噪 c语言,一维信号小波阈值去噪
- apache自定义虚拟主机日志格式
- 2019中国基金业金融科技发展白皮书
- fpt指的是什么_ftp是指的什么?
- 在html语言中读取txt文件中的内容,Javascript写入txt和读取txt文件示例详解
- spring boot 2 整合 j2Cache
- 洛谷 P3382(三分查找凹点和凸点)
- JSP 页面缓存以及清除缓存
- Tomcat之Directory Listing
- 计算机组成原理笔记|02运算方法和运算器
- Linux-Socket实现模拟群聊(多人聊天室)
- OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000005c0000000, 5726797824, 0) failed;
- 企业微信消息推送卡片按钮互动的使用
- linux ubuntu 命令行高亮
- Peter Meer
- Java乘船_pokemmo神奥哪里坐船
- 跟庄买股票得新技巧(第三弹)集合竞价战法
- 理解索引:MySQL执行计划详细介绍
热门文章
- R语言构建catboost模型:构建catboost模型并基于网格搜索获取最优模型参数(Select hyperparameters)、计算特征重要度
- 简述神经网络的训练过程?
- python使用箱图法和业务规则进行异常数据处理并检查预测使用的数据特征是否有字段缺失的情况并补齐
- 固态硬盘上安装centos_固态硬盘使用f2fs作为根分区安装linux
- 第三代测序技术的主要特点及其在病毒基因组研究中的应用
- Basic local alignment search tool (BLAST)
- python configparser 参数 用法
- 【ES6】对象的拓展
- 图像分割--PixelNet: Representation of the pixels, by the pixels, and for the pixels
- pdf转换为html5,PDF转换为HTML5的四种方式