Skip to content

Commit 301b841

Browse files
committed
fd
1 parent 5aa7fa0 commit 301b841

File tree

2 files changed

+65
-48
lines changed

2 files changed

+65
-48
lines changed

leetcode/solution/src/KthSmallestElementInBST.java

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,58 @@ public int kthSmallest(TreeNode root, int k) {
3232
throw new IllegalStateException();
3333
}
3434

35-
/**
36-
* Morris
37-
*/
38-
private int kthSmallest2(TreeNode root, int k) {
39-
TreeNode temp;
35+
public int kthSmallest2(TreeNode root, int k) {
36+
TreeNodeWithCount rootWithCount = buildTreeWithCount(root);
37+
return kthSmallest(rootWithCount, k);
38+
}
4039

41-
while (root != null) {
42-
if (root.left == null) {
43-
if (--k == 0) {
44-
return root.val;
45-
}
46-
root = root.right;
40+
public int kthSmallest(TreeNodeWithCount root, int k) {
41+
if (root == null) {
42+
return -1;
43+
}
44+
45+
if (root.left != null) {
46+
if (k <= root.left.count) {
47+
return kthSmallest(root.left, k);
48+
} else if (k == root.left.count + 1) {
49+
return root.val;
4750
} else {
48-
temp = root.left;
49-
while (temp.right != null && temp.right != root) {
50-
temp = temp.right;
51-
}
52-
if (temp.right == null) {
53-
temp.right = root;
54-
root = root.left;
55-
} else {
56-
temp.right = null;
57-
if (--k == 0) {
58-
return root.val;
59-
}
60-
root = root.right;
61-
}
51+
return kthSmallest(root.right, k - root.left.count - 1);
6252
}
6353
}
64-
throw new IllegalStateException();
54+
55+
if (k == 1) {
56+
return root.val;
57+
} else {
58+
return kthSmallest(root.right, k - 1);
59+
}
60+
}
61+
62+
private TreeNodeWithCount buildTreeWithCount(TreeNode root) {
63+
if (root == null) {
64+
return null;
65+
}
66+
TreeNodeWithCount nroot = new TreeNodeWithCount(root.val);
67+
nroot.left = buildTreeWithCount(root.left);
68+
nroot.right = buildTreeWithCount(root.right);
69+
70+
if (nroot.left != null) {
71+
nroot.count += nroot.left.count;
72+
}
73+
74+
if (nroot.right != null) {
75+
nroot.count += nroot.right.count;
76+
}
77+
return nroot;
78+
}
79+
80+
class TreeNodeWithCount {
81+
TreeNodeWithCount left, right;
82+
int count, val;
83+
84+
TreeNodeWithCount(int val) {
85+
this.val = val;
86+
this.count = 1;
87+
}
6588
}
6689
}

leetcode/src/Main.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,23 @@ public class Main {
66

77
public static class Solution {
88

9-
public int longestUnivaluePath(TreeNode root) {
10-
int[] res = new int[1];
11-
dfs(root, res);
12-
return res[0];
13-
}
14-
15-
private int dfs(TreeNode node, int[] res) {
16-
if (node == null) {
17-
return 0;
18-
}
19-
int left = dfs(node.left, res);
20-
int right = dfs(node.right, res);
21-
22-
int lval = 0, rval = 0;
23-
24-
if (node.left != null && node.left.val == node.val) {
25-
lval = left + 1;
26-
}
27-
if (node.right != null && node.right.val == node.val) {
28-
rval = right + 1;
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+
18+
if (--k == 0) {
19+
return root.val;
20+
}
21+
22+
root = root.right;
23+
}
2924
}
30-
res[0] = Math.max(res[0], lval + rval);
31-
return Math.max(lval, rval);
25+
return -1;
3226
}
3327
}
3428

0 commit comments

Comments
 (0)