632. 二叉树的最大节点(java)
2018-01-17 23:51
211 查看
题目:
在二叉树中寻找值最大的节点并返回样例给出如下一棵二叉树:
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/*
* @param root: the root of tree
* @return: the max node
*/
public TreeNode maxNode(TreeNode root) {
// write your code here
int max;
TreeNode T=new TreeNode(0);
T=root;
if(root!=null)
{
max=root.val;
TreeNode R=new TreeNode(0);
R=root;
if(R.left!=null)
{
TreeNode Left=new TreeNode(0);
Left=maxNode(R.left);
if(Left.val>max)
{
max=Left.val;
T=Left;
}
}
if(R.right!=null)
{
TreeNode Right=new TreeNode(0);
Right=maxNode(R.right);
if(Right.val>max)
{
max=Right.val;
T=Right;
}
}
}
return T;
}
}
在二叉树中寻找值最大的节点并返回样例给出如下一棵二叉树:
1 / \ -5 2 / \ / \ 0 3 -4 -5返回值为
3的节点。代码:/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/*
* @param root: the root of tree
* @return: the max node
*/
public TreeNode maxNode(TreeNode root) {
// write your code here
int max;
TreeNode T=new TreeNode(0);
T=root;
if(root!=null)
{
max=root.val;
TreeNode R=new TreeNode(0);
R=root;
if(R.left!=null)
{
TreeNode Left=new TreeNode(0);
Left=maxNode(R.left);
if(Left.val>max)
{
max=Left.val;
T=Left;
}
}
if(R.right!=null)
{
TreeNode Right=new TreeNode(0);
Right=maxNode(R.right);
if(Right.val>max)
{
max=Right.val;
T=Right;
}
}
}
return T;
}
}
相关文章推荐
- 【LintCode-632】二叉树的最大节点(Java实现)
- Java二叉树排序及任意两点个节点间的最大距离
- java面试题:如何求二叉树中节点的最大距离+层序遍历
- [C语言]求二叉树节点最大距离(C、JAVA 递归、非递归)
- java 二叉树(七)获取左子树中的最大节点
- LintCode之632 二叉树最大节点
- LintCode:二叉树的最大节点632
- java 求二叉树中节点的最大距离.
- lintcode 632. 二叉树的最大节点
- LintCode入门练习——632. 二叉树的最大节点
- LintCode入门练习——632. 二叉树的最大节点
- Lintcode 入门-632. 二叉树的最大节点
- 【入门】lintcode 632: 二叉树的最大节点
- LintCode-(632)二叉树的最大节点
- 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离
- Java:求二叉树中节点的最大距离
- 二叉树中节点的最大距离(java)
- 【LintCode 入门】632. 二叉树的最大节点
- 二叉树的最大节点
- 《编程之美: 求二叉树中节点的最大距离》的另一个解法(转)