File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11/**
22 * https://leetcode.com/articles/construct-string-from-binary-tree/
33 */
4+
5+ /**
6+ * 这题不难,主要是搞清楚题目意思
7+ * 即返回root + (左子树) + (右子树)
8+ * 不过要注意几个特殊情况,如果左右子树都为null,则只返回root
9+ * 如果左子树为null,右子树非null,则返回root + () + (右子树)
10+ * 如果左子树非null,右子树为null,则返回root + (左子树)
11+ */
412public class ConstructStringFromBinaryTree {
513
614 /**
Original file line number Diff line number Diff line change @@ -6,29 +6,12 @@ public class Main {
66
77 public static class Solution {
88
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 ();
179
18- if (--k == 0 ) {
19- return root .val ;
20- }
2110
22- root = root .right ;
23- }
24- }
25- return -1 ;
26- }
2711 }
2812
2913 public static void main (String [] args ) {
3014 Solution s = new Solution ();
31- TreeNode root1 = new TreeNode (1 );
32- System .out .println (f );
15+ System .out .println (a );
3316 }
3417}
You can’t perform that action at this time.
0 commit comments