LintCode二叉树的最大节点
来源:互联网 发布:淘宝大学金牌讲师小飞 编辑:程序博客网 时间:2024/06/14 14:09
本系列的博客将围绕在LintCode网站上的算法题进行编写,主要记录一下在刷题过程中的一些思路、想法,遇到困难时也会参考一些网上的资源。
目录
- 目录
- 题目描述
- 解题思路
- 代码块
题目描述
在二叉树中寻找值最大的节点并返回。
样例:
1
/ \
-5 2
/ \ / \
0 3 -4 -5
解题思路
求解这个问题就像是求解一个数组的最大值一样,要将当前的最大的依次与后面的值进行比较,从而得到最大的值,唯一的不同的点就是这里使用的是树结构,所以要依次比较左右子树。
代码块
具体的实现代码如下:
public class Solution { /** * @param root the root of binary tree * @return the max ndoe */ public TreeNode maxNode(TreeNode root) { // Write your code here if(root == null){ return null; } TreeNode max = root; TreeNode temp; if(root.left == null && root.right == null){ max = root; } if(root.left != null){ temp = maxNode(root.left); if(max.val < temp.val){ max = temp; } } if(root.right != null){ temp = maxNode(root.right); if(max.val < temp.val){ max = temp; } } return max; }}
阅读全文
0 0
- LintCode 二叉树的最大节点
- LintCode-二叉树的最大节点
- lintcode 二叉树的最大节点
- 二叉树的最大节点-LintCode
- lintcode(632)二叉树的最大节点
- [Lintcode]二叉树的最大节点
- lintcode--二叉树的最大节点
- LintCode:二叉树的最大节点632
- LintCode二叉树的最大节点
- LintCode 二叉树的最大节点
- 二叉树的最大节点-LintCode
- LintCode-(632)二叉树的最大节点
- LintCode 二叉树的最大节点
- Lintcode:二叉树的最大节点
- 二叉树最大节点-lintCode
- lintcode——二叉树的最大节点
- (lintcode)第632题二叉树的最大节点
- 【LintCode-632】二叉树的最大节点(Java实现)
- 一篇面经(BAT面试)
- [Python]网络数据采集概述(1)—页面访问及页面元素的解析
- iptables 优化
- NYOJ题目289/456/49-01背包问题汇总
- mysql 入门第二天 涉及连表查询
- LintCode二叉树的最大节点
- 微信小程序------小程序初步学习
- hibernate缓存
- Hadoop Shell命令
- 通用数据存储结构
- 最小生成树prim和kruskal算法
- 学习muduo(类之间的函数调用,未完成)
- matlab提示Undefined operator '*' for input arguments of type 'cell'.
- flume简介