[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/feixiangcode/algorithm/master/Week_02/id_27/LeetCode_671_027.java [Back]  [Original]

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {


  /*
	   * firstsecond65535
	   * firstsecond
	   * firstfirstsecondfirst
	   * firstsecondsecond
	   * 
	   * second65535-1second
	   *
	   */


	class CustomLong{
		public CustomLong(Long value) {
			super();
			this.value = value;
		}

		Long value;

		@Override
		public String toString() {
			return "CustomLong [value=" + value + "]";
		}

	}

	public int findSecondMinimumValue(TreeNode root) {

		CustomLong first = new CustomLong(Long.MAX_VALUE),second = new CustomLong(Long.MAX_VALUE);
		twoMinimum(root,first,second);
		if(second.value != Long.MAX_VALUE) return second.value.intValue();
		else return -1;
	}

	private void twoMinimum(TreeNode root,CustomLong first,CustomLong second) {

		if (root == null) return;

		if(root.val < first.value) {
			second.value = first.value;
			first.value = (long)root.val;
		}
		else if(root.val > first.value && root.val < second.value){
			second.value = (long)root.val;
		}

		twoMinimum(root.left,first,second);
		twoMinimum(root.right,first,second);

	}

	public void test(CustomLong in) {
		in.value =  10L;
	}

	public static void main(String[] args) {
		/*CustomInt in = new Solution().new CustomInt(2);
		new Solution().test(in);
		System.out.println(in);*/

		TreeNode n1 = new TreeNode(2);
		TreeNode n2 = new TreeNode(2);
		TreeNode n3 = new TreeNode(2147483647);
		n1.left = n2;
		n1.right = n3;
		System.out.println(new Solution().findSecondMinimumValue(n1));

	}


}

Web Proxy Viewer  |  New URL  |  Original Page