劍指offer-二叉查找樹的第 K 個結點
阿新 • • 發佈:2019-02-01
solution right pan == col node val root left
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { int count = 0; int result = Integer.MIN_VALUE; public int kthSmallest(TreeNode root, intk) { traverse(root, k); return result; } public void traverse(TreeNode root, int k) { if(root == null) return; traverse(root.left, k); count++; if(count == k) result = root.val; traverse(root.right, k); } }
劍指offer-二叉查找樹的第 K 個結點