Java inorder traversal


  • 1
    public class Solution {
        TreeNode pre = null;
        int min = Integer.MAX_VALUE;
        public int getMinimumDifference(TreeNode root) {
            inOrder(root);
            return min;
        }
        
        private void inOrder(TreeNode root) {
            if (root == null)   return;
            inOrder(root.left);
            if (pre != null) {
                min = Math.min(min, root.val - pre.val);
            }
            pre = root;
            inOrder(root.right);
        }
    }
    

Log in to reply
 

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