public int maxTreeNode(TreeNode root)
{
if(root.left == null && root.right == null)
return root.val;
else if(root.left != null && root.right == null)
return root.val > maxTreeNode(root.left) ? root.val : maxTreeNode(root.left);
else if(root.left == null && root.right != null)
return root.val > maxTreeNode(root.right) ? root.val : maxTreeNode(root.right);
else
{
if(root.val > maxTreeNode(root.left))
return root.val > maxTreeNode(root.right) ? root.val : maxTreeNode(root.right);
else
return maxTreeNode(root.left) > maxTreeNode(root.right) ? maxTreeNode(root.left) : maxTreeNode(root.right);
}
}
数据结构--求二叉树节点最大值
最新推荐文章于 2022-10-05 16:56:51 发布