issues
search
Shawngbk
/
Leecode
Questions of Leecode
0
stars
0
forks
source link
230. Kth Smallest Element in a BST
#146
Open
Shawngbk
opened
7 years ago
Shawngbk
commented
7 years ago
使用中序遍历复杂度O(n)
/**
Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
} */ public class Solution { public int kthSmallest(TreeNode root, int k) { List
list = new ArrayList
(); helper(root, list); return list.get(k-1); } private void helper(TreeNode root, List
list) { if(root != null) { if(root.left != null) { helper(root.left, list); } list.add(root.val); if(root.right != null) { helper(root.right, list); } } } }
Shawngbk
commented
7 years ago
/**