二叉树最大节点
Java版:
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 root;
TreeNode left = maxNode(root.left);
TreeNode right = maxNode(root.right);
return max(root, max(left, right));
}
TreeNode max(TreeNode a, TreeNode b) {
if (a == null)
return b;
if (b == null)
return a;
if (a.val > b.val) {
return a;
}
return b;
}
}
本文介绍了一种求解二叉树中最大节点值的算法实现。通过递归方式遍历左右子树,并比较各节点值来确定最大值。文章包含完整的Java代码示例。
1402

被折叠的 条评论
为什么被折叠?



