Skip to content

Commit c7fb61e

Browse files
author
wb-hjk570755
committed
二叉查找树的后继节点
1 parent e33d653 commit c7fb61e

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.blankj.easy._053;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/4/3
10+
*/
11+
public class Kuai_shou_test {
12+
public static void main(String[] args) {
13+
String a = "abc";
14+
for (int i=0;i<a.length();i++){
15+
System.out.println(a.charAt(i));
16+
System.out.println((int)a.charAt(i));
17+
}
18+
System.out.println(Integer.MAX_VALUE);
19+
}
20+
21+
public int count(int[][] a,int x,int y){
22+
23+
return 1;
24+
}
25+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.blankj.myself;
2+
3+
import java.util.Objects;
4+
import java.util.Stack;
5+
6+
/**
7+
* Description:
8+
* Copyright: Copyright (c) 2012
9+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
10+
*
11+
* @author huangjk
12+
* @version 1.0 2020/5/18
13+
*/
14+
15+
public class TreePostNode {
16+
17+
public static void main(String[] args) {
18+
19+
}
20+
21+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
22+
Stack<TreeNode> stack = new Stack<>();
23+
TreeNode result = null;
24+
if(Objects.nonNull(p.right)){
25+
result = p.right;
26+
while (Objects.nonNull(result.left)){
27+
result = result.left;
28+
}
29+
}else {
30+
stack.add(root);
31+
TreeNode temp = root;
32+
Boolean findFlag = false;
33+
while (!findFlag){
34+
if(p.val>temp.val){
35+
temp = temp.right;
36+
stack.add(temp);
37+
38+
}else if(p.val<temp.val){
39+
temp = temp.left;
40+
stack.add(temp);
41+
42+
}else {
43+
findFlag = true;
44+
}
45+
}
46+
findFlag = false;
47+
TreeNode child = p;
48+
while (stack.size()>0&&!findFlag){
49+
TreeNode parent = stack.pop();
50+
if(child == parent.left){
51+
result = parent;
52+
findFlag = true;
53+
}else {
54+
child = parent;
55+
}
56+
}
57+
58+
}
59+
return result;
60+
}
61+
62+
public class TreeNode {
63+
int val;
64+
TreeNode left;
65+
TreeNode right;
66+
TreeNode(int x) { val = x; }
67+
}
68+
}

0 commit comments

Comments
 (0)