/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int secondVal = Integer.MAX_VALUE;
//flag用于标识secondVal发生了改变
private boolean flag = false;
public int findSecondMinimumValue(TreeNode root) {
if(root == null) return -1;
return findSecond(root,root.val);
}
public int findSecond(TreeNode root,int min){
if(root == null ) return -1;
int tmpVal = 0;
if(root.val != min)
{
tmpVal = root.val;
if(tmpVal<=secondVal) secondVal = tmpVal;
flag =true;
}
findSecond(root.left,min);
findSecond(root.right,min);
if(secondVal != min &&flag) return secondVal;
else return -1;
}
}
671. 二叉树中第二小的节点(树)
最新推荐文章于 2021-04-01 09:05:06 发布