Java-Inorder Traversal


  • 0
    N

    public class Solution {
    TreeNode previousNodeValue;
    int difference = Integer.MAX_VALUE;

    	public int getMinimumDifference(TreeNode root) {
    		// Traverse the Tree in In-order fashion
    		if (root != null) {
    			getMinimumDifference(root.left);
    			if (previousNodeValue != null
    					&& difference > root.val - previousNodeValue.val) {
    				difference = root.val - previousNodeValue.val;
    			}
    			previousNodeValue = root;
    			getMinimumDifference(root.right);
    		}
    
    		return difference;
    	}

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.