/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { List<int> list = new List<int>(); void postOrder(TreeNode Node) { if (Node != null) { list.Add(Node.val); } if (Node != null && Node.left != null) { postOrder(Node.left); } if (Node != null && Node.right != null) { postOrder(Node.right); } } public int GetMinimumDifference(TreeNode root) { postOrder(root); list = list.OrderBy(x => x).ToList(); var min = Int32.MaxValue; for (int i = 0; i < list.Count - 1; i++) { min = Math.Min(min, Math.Abs(list[i] - list[i + 1])); } Console.WriteLine(min); return min; } }
https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description