Skip to content

Commit e063607

Browse files
committed
delete leaf having value k
1 parent 7f9f6ea commit e063607

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
class Node {
3+
constructor(data) {
4+
this.data = data;
5+
this.leftNode = this.rightNode = null;
6+
}
7+
}
8+
9+
function isLeaf(node) {
10+
if (node == null) return false;
11+
if (node.leftNode == null && node.rightNode == null) return true;
12+
return false;
13+
}
14+
15+
function deleteLeaf(root, k) {
16+
if (root == null) return null;
17+
root.leftNode = deleteLeaf(root.leftNode, k);
18+
root.rightNode = deleteLeaf(root.rightNode, k);
19+
if (root.data == k && isLeaf(root)) return null;
20+
return root;
21+
}
22+
23+
function preOrderDisplay(root) {
24+
if (root == null) return null;
25+
console.log(root.data);
26+
preOrderDisplay(root.leftNode);
27+
preOrderDisplay(root.rightNode);
28+
}
29+
30+
//level - 1
31+
let tree = new Node(6);
32+
33+
// level - 2
34+
tree.leftNode = new Node(5);
35+
tree.rightNode = new Node(4);
36+
37+
// level - 3
38+
tree.leftNode.leftNode = new Node(5);
39+
tree.leftNode.rightNode = new Node(5);
40+
41+
tree.rightNode.rightNode = new Node(5);
42+
// 6
43+
// / \
44+
// 5 4
45+
// / \ \
46+
// 5 5 5
47+
let k = 5;
48+
let newTree = deleteLeaf(tree, k)
49+
preOrderDisplay(newTree);

0 commit comments

Comments
 (0)