Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5787c1

Browse files
Merge pull request doocs#221 from huaxu1024/master
Create Solution.java
2 parents dce9fe8 + f3119be commit a5787c1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
12+
public int kthSmallest(TreeNode root, int k) {
13+
return inOrderTraversal(root, new ArrayList<Integer>()).get(k - 1);
14+
}
15+
16+
public List<Integer> inOrderTraversal(TreeNode root, List<Integer> list) {
17+
if (root == null)
18+
return list;
19+
20+
inOrderTraversal(root.left, list);
21+
list.add(root.val);
22+
inOrderTraversal(root.right, list);
23+
return list;
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /