Given a 0-indexed integer 2D arraynodes, your task is to determine if the given array represents the preorder traversal of some binary tree.
For each index i, nodes[i] = [id, parentId], where id is the id of the node at the index i and parentId is the id of its parent in the tree (if the node has no parent, then parentId == -1).
Return trueif the given arrayrepresents the preorder traversal of some tree, andfalseotherwise.
Note: the preorder traversal of a tree is a recursive way to traverse a tree in which we first visit the current node, then we do the preorder traversal for the left child, and finally, we do it for the right child.
Input: nodes =[[0,-1],[1,0],[2,0],[3,2],[4,2]]Output: trueExplanation: The given nodes make the tree in the picture below.We can show that thisis the preorder traversal of the tree, first we visit node 0, then we do the preorder traversal of the right child which is[1], then we do the preorder traversal of the left child which is[2,3,4].
Example 2:
1
2
3
4
5
Input: nodes =[[0,-1],[1,0],[2,0],[3,1],[4,1]]Output: falseExplanation: The given nodes make the tree in the picture below.For the preorder traversal, first we visit node 0, then we do the preorder traversal of the right child which is[1,3,4], but we can see that in the given order,2 comes between 1 and 3, so, it's not the preorder traversal of the tree.
Constraints:
1 <= nodes.length <= 10^5
nodes[i].length == 2
0 <= nodes[i][0] <= 10^5
-1 <= nodes[i][1] <= 10^5
The input is generated such that nodes make a binary tree.
To check if the given array is a valid preorder traversal of a binary tree, we simulate the traversal using a stack. We ensure that each node’s parent appears before it and that the parent-child relationships are consistent with preorder traversal.
classSolution {
public:bool validateBinaryTreeNodes(vector<vector<int>>& nodes) {
int n = nodes.size();
unordered_map<int, int> idx;
for (int i =0; i < n; ++i) idx[nodes[i][0]] = i;
stack<int> st;
for (int i =0; i < n; ++i) {
int id = nodes[i][0], par = nodes[i][1];
if (par ==-1) st.push(id);
else {
while (!st.empty() && st.top() != par) st.pop();
if (st.empty()) return false;
st.push(id);
}
}
return true;
}
};
funcvalidateBinaryTreeNodes(nodes [][]int) bool {
idx:=map[int]int{}
fori, v:=rangenodes {
idx[v[0]] = i }
st:= []int{}
for_, v:=rangenodes {
id, par:=v[0], v[1]
ifpar==-1 {
st = append(st, id)
} else {
for len(st) > 0&&st[len(st)-1] !=par {
st = st[:len(st)-1]
}
if len(st) ==0 {
returnfalse }
st = append(st, id)
}
}
returntrue}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicbooleanvalidateBinaryTreeNodes(int[][] nodes) {
int n = nodes.length;
Map<Integer, Integer> idx =new HashMap<>();
for (int i = 0; i < n; ++i) idx.put(nodes[i][0], i);
Deque<Integer> st =new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
int id = nodes[i][0], par = nodes[i][1];
if (par ==-1) st.push(id);
else {
while (!st.isEmpty() && st.peek() != par) st.pop();
if (st.isEmpty()) returnfalse;
st.push(id);
}
}
returntrue;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funvalidateBinaryTreeNodes(nodes: Array<IntArray>): Boolean {
val idx = mutableMapOf<Int, Int>()
for (i in nodes.indices) idx[nodes[i][0]] = i
val st = ArrayDeque<Int>()
for (v in nodes) {
val id = v[0]; val par = v[1]
if (par == -1) st.addLast(id)
else {
while (st.isNotEmpty() && st.last() != par) st.removeLast()
if (st.isEmpty()) returnfalse st.addLast(id)
}
}
returntrue }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
defvalidate_binary_tree_nodes(nodes: list[list[int]]) -> bool:
idx = {id: i for i, (id, _) in enumerate(nodes)}
st = []
for id, par in nodes:
if par ==-1:
st.append(id)
else:
while st and st[-1] != par:
st.pop()
ifnot st:
returnFalse st.append(id)
returnTrue
use std::collections::HashMap;
fnvalidate_binary_tree_nodes(nodes: Vec<Vec<i32>>) -> bool {
letmut idx = HashMap::new();
for (i, v) in nodes.iter().enumerate() {
idx.insert(v[0], i);
}
letmut st =vec![];
for v in nodes {
let id = v[0]; let par = v[1];
if par ==-1 {
st.push(id);
} else {
whilelet Some(&top) = st.last() {
if top == par { break; }
st.pop();
}
if st.is_empty() { returnfalse; }
st.push(id);
}
}
true}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionvalidateBinaryTreeNodes(nodes: number[][]):boolean {
constidx: Record<number, number> = {};
for (leti=0; i<nodes.length; ++i) idx[nodes[i][0]] =i;
constst: number[] = [];
for (const [id, par] ofnodes) {
if (par===-1) st.push(id);
else {
while (st.length&&st[st.length-1] !==par) st.pop();
if (!st.length) returnfalse;
st.push(id);
}
}
returntrue;
}