File tree Expand file tree Collapse file tree 2 files changed +9
-18
lines changed Expand file tree Collapse file tree 2 files changed +9
-18
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
2
* https://leetcode.com/articles/construct-string-from-binary-tree/
3
3
*/
4
+
5
+ /**
6
+ * 这题不难,主要是搞清楚题目意思
7
+ * 即返回root + (左子树) + (右子树)
8
+ * 不过要注意几个特殊情况,如果左右子树都为null,则只返回root
9
+ * 如果左子树为null,右子树非null,则返回root + () + (右子树)
10
+ * 如果左子树非null,右子树为null,则返回root + (左子树)
11
+ */
4
12
public class ConstructStringFromBinaryTree {
5
13
6
14
/**
Original file line number Diff line number Diff line change @@ -6,29 +6,12 @@ public class Main {
6
6
7
7
public static class Solution {
8
8
9
- public int kthSmallest (TreeNode root , int k ) {
10
- Stack <TreeNode > stack = new Stack <>();
11
- while (!stack .isEmpty () || root != null ) {
12
- if (root != null ) {
13
- stack .push (root );
14
- root = root .left ;
15
- } else {
16
- root = stack .pop ();
17
9
18
- if (--k == 0 ) {
19
- return root .val ;
20
- }
21
10
22
- root = root .right ;
23
- }
24
- }
25
- return -1 ;
26
- }
27
11
}
28
12
29
13
public static void main (String [] args ) {
30
14
Solution s = new Solution ();
31
- TreeNode root1 = new TreeNode (1 );
32
- System .out .println (f );
15
+ System .out .println (a );
33
16
}
34
17
}
You can’t perform that action at this time.
0 commit comments