20
10 20 50 -1 60 -1 -1 30 70 -1 80 -1 90 -1 -1 40 100 -1 110 -1 -1 -1
O/P: true
import java.io.*;
import java.util.*;
public class Main {
private static class Node {
int data;
ArrayList<Node> children = new ArrayList<>();
}
public static void display(Node node) {
String str = node.data + " -> ";
for (Node child : node.children) {
str += child.data + ", ";
}
str += ".";
System.out.println(str);
for (Node child : node.children) {
display(child);
}
}
public static Node construct(int[] arr) {
Node root = null;
Stack<Node> st = new Stack<>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == -1) {
st.pop();
} else {
Node t = new Node();
t.data = arr[i];
if (st.size() > 0) {
st.peek().children.add(t);
} else {
root = t;
}
st.push(t);
}
}
return root;
}
public static int size(Node node) {
int s = 0;
for (Node child : node.children) {
s += size(child);
}
s += 1;
return s;
}
public static int max(Node node) {
int m = Integer.MIN_VALUE;
for (Node child : node.children) {
int cm = max(child);
m = Math.max(m, cm);
}
m = Math.max(m, node.data);
return m;
}
public static int height(Node node) {
int h = -1;
for (Node child : node.children) {
int ch = height(child);
h = Math.max(h, ch);
}
h += 1;
return h;
}
public static boolean IsSymmetric(Node node) {
// write your code here
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(values[i]);
}
Node root = construct(arr);
boolean sym = IsSymmetric(root);
System.out.println(sym);
// display(root);
}
}
No comments:
Post a Comment