1. 程式人生 > >劍指offer-二叉查找樹的第 K 個結點

劍指offer-二叉查找樹的第 K 個結點

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, int
k) { 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 個結點