-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostorderSuccessor.java
More file actions
52 lines (40 loc) · 1.17 KB
/
postorderSuccessor.java
File metadata and controls
52 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.* ;
import java.io.*;
/*******************************************************
Following is the BinaryTreeNode class structure
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
public BinaryTreeNode(T data) {
this.data = data;
}
}
*******************************************************/
public class Solution
{
public static int postOrderSuccessor(BinaryTreeNode<Integer> root, int M)
{
if(root==null){
return -1;
}
ArrayList<Integer> arr = new ArrayList<>();
postOrder(root , arr);
for (int i = 0; i < arr.size(); i++) {
int n = arr.get(i);
if (n == M && i < arr.size() - 1) {
int ans = arr.get(i + 1);
return ans;
}
}
return -1;
}
public static void postOrder(BinaryTreeNode<Integer> root , ArrayList<Integer> arr){
if (root == null) {
return;
}
postOrder(root.left, arr);
postOrder(root.right, arr);
arr.add(root.data);
}
}