From a4c817373e80c65ca773a48155e833b0ec21f719 Mon Sep 17 00:00:00 2001 From: Stone Lyu Date: Tue, 24 Dec 2019 15:26:47 +0800 Subject: [PATCH] feat: #230 add java solution(#249) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * correct typos * Add translations * 新增HTTP/1.0, HTTP/1.1, HTTP/2的对比 * 增加HTTP/1.1和HTTP/2的性能对比的Demo * 翻译大部分基础数据结构,但是REACT部分没有翻译 * update description of `HTTP/1.0` * add WIP tag, update some translation details * change 'fundamental' to 'basic' * remove some trivial details about HTTP/1.0 * error correction * translation for DP * translation for string problems * error correction * correct wrongly typed * translation for binary-tree-traversal.md * correct wrongly typed * update README.en.md * update description of Huffman encoding * update file path of Binary-tree-traversal-en.md. * correct wrongly typed * update paths of thinkings * translation for basic-algorithm.md (Draft) * translation for bloom-filter.md * add java solution with inorder traversing * Update 230.kth-smallest-element-in-a-bst.md Co-authored-by: lucifer --- problems/230.kth-smallest-element-in-a-bst.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/230.kth-smallest-element-in-a-bst.md b/problems/230.kth-smallest-element-in-a-bst.md index 566405c90..b11e07d78 100644 --- a/problems/230.kth-smallest-element-in-a-bst.md +++ b/problems/230.kth-smallest-element-in-a-bst.md @@ -56,6 +56,8 @@ What if the BST is modified (insert/delete operations) often and you need to fin 解法一: +JavaScript Code: + ```js @@ -107,9 +109,44 @@ var kthSmallest = function(root, k) { }; ``` +Java Code: + +```java +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +private int count = 1; +private int res; + +public int KthSmallest (TreeNode root, int k) { + inorder(root, k); + return res; +} + +public void inorder (TreeNode root, int k) { + if (root == null) return; + + inorder(root.left, k); + + if (count++ == k) { + res = root.val; + return; + } + + inorder(root.right, k); +} +``` 解法二: +JavaScript Code: + ```js /**