Skip to content

Commit 66347d5

Browse files
authored
Merge pull request #8 from rvguradiya/full-binary-tree-conversion
convert a binary tree to full tree by removing node that have only on…
2 parents 114a6d2 + 74f8dc6 commit 66347d5

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class TreeNode {
2+
int value;
3+
TreeNode left, right;
4+
5+
public TreeNode(int value) {
6+
this.value = value;
7+
left = right = null;
8+
}
9+
}
10+
11+
public class Approach {
12+
13+
// Method to convert the binary tree to a full binary tree
14+
public static TreeNode convertToFullBinaryTree(TreeNode node) {
15+
TreeNode tempL = null, tempR = null, temp = null;
16+
if (node.left == null && node.right == null) {
17+
return node;
18+
}
19+
if (node.left != null) {
20+
tempL = convertToFullBinaryTree(node.left);
21+
}
22+
if (node.right != null) {
23+
tempR = convertToFullBinaryTree(node.right);
24+
}
25+
if (node.left != null && node.right != null) {
26+
node.left = tempL;
27+
node.right = tempR;
28+
return node;
29+
}
30+
return tempL == null ? tempR : tempL;
31+
}
32+
33+
// Method to print the tree in InOrder (used for testing)
34+
public static void printInOrder(TreeNode node) {
35+
if (node == null) {
36+
return;
37+
}
38+
printInOrder(node.left);
39+
System.out.print(node.value + " ");
40+
printInOrder(node.right);
41+
}
42+
43+
public static void main(String[] args) {
44+
// Construct the binary tree
45+
TreeNode root = new TreeNode(0);
46+
root.left = new TreeNode(1);
47+
root.right = new TreeNode(2);
48+
root.left.left = new TreeNode(3);
49+
root.left.left.right = new TreeNode(5);
50+
root.right.right = new TreeNode(4);
51+
root.right.right.left = new TreeNode(6);
52+
root.right.right.right = new TreeNode(7);
53+
54+
System.out.println("Original Binary Tree (InOrder):");
55+
printInOrder(root);
56+
System.out.println();
57+
58+
// Convert to full binary tree
59+
TreeNode fullTree = convertToFullBinaryTree(root);
60+
61+
System.out.println("Full Binary Tree (InOrder):");
62+
printInOrder(fullTree);
63+
System.out.println();
64+
}
65+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
This problem was asked by **Yahoo**.
2+
3+
Recall that a full binary tree is one in which each node is either a leaf node, or has two children. Given a binary tree, convert it to a full one by removing nodes with only one child.
4+
5+
For example, given the following tree:
6+
7+
```graph
8+
9+
0
10+
/ \
11+
1 2
12+
/ \
13+
3 4
14+
\ / \
15+
5 6 7
16+
```
17+
18+
You should convert it to:
19+
20+
```graph
21+
0
22+
/ \
23+
5 4
24+
/ \
25+
6 7
26+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class TreeNode {
2+
constructor(value) {
3+
this.value = value;
4+
this.left = null;
5+
this.right = null;
6+
}
7+
}
8+
9+
// Method to convert the binary tree to a full binary tree
10+
function convertToFullBinaryTree(node) {
11+
let tempL = null,
12+
tempR = null;
13+
14+
// If the node is a leaf node, return it
15+
if (node.left === null && node.right === null) {
16+
return node;
17+
}
18+
19+
// Recursively process the left and right subtrees
20+
if (node.left !== null) {
21+
tempL = convertToFullBinaryTree(node.left);
22+
}
23+
if (node.right !== null) {
24+
tempR = convertToFullBinaryTree(node.right);
25+
}
26+
27+
// If the node has two children, keep the node
28+
if (node.left !== null && node.right !== null) {
29+
node.left = tempL;
30+
node.right = tempR;
31+
return node;
32+
}
33+
34+
// If the node has only one child, return the non-null child
35+
return tempL === null ? tempR : tempL;
36+
}
37+
38+
// Method to print the tree in InOrder (used for testing)
39+
function printInOrder(node) {
40+
if (node === null) return;
41+
42+
printInOrder(node.left);
43+
process.stdout.write(node.value + " ");
44+
printInOrder(node.right);
45+
}
46+
47+
// Main function to test the functionality
48+
function main() {
49+
// Construct the binary tree
50+
let root = new TreeNode(0);
51+
root.left = new TreeNode(1);
52+
root.right = new TreeNode(2);
53+
root.left.left = new TreeNode(3);
54+
root.left.left.right = new TreeNode(5);
55+
root.right.right = new TreeNode(4);
56+
root.right.right.left = new TreeNode(6);
57+
root.right.right.right = new TreeNode(7);
58+
59+
console.log("Original Binary Tree (InOrder):");
60+
printInOrder(root);
61+
console.log();
62+
63+
// Convert to full binary tree
64+
let fullTree = convertToFullBinaryTree(root);
65+
66+
console.log("Full Binary Tree (InOrder):");
67+
printInOrder(fullTree);
68+
console.log();
69+
}
70+
71+
main();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class TreeNode:
2+
def __init__(self, value):
3+
self.value = value
4+
self.left = None
5+
self.right = None
6+
7+
# Method to convert the binary tree to a full binary tree
8+
def convert_to_full_binary_tree(node):
9+
tempL = None
10+
tempR = None
11+
12+
# If the node is a leaf node, return it
13+
if node.left is None and node.right is None:
14+
return node
15+
16+
# Recursively process the left and right subtrees
17+
if node.left is not None:
18+
tempL = convert_to_full_binary_tree(node.left)
19+
if node.right is not None:
20+
tempR = convert_to_full_binary_tree(node.right)
21+
22+
# If the node has two children, keep the node
23+
if node.left is not None and node.right is not None:
24+
node.left = tempL
25+
node.right = tempR
26+
return node
27+
28+
# If the node has only one child, return the non-null child
29+
return tempL if tempL is not None else tempR
30+
31+
# Method to print the tree in InOrder (used for testing)
32+
def print_in_order(node):
33+
if node is None:
34+
return
35+
print_in_order(node.left)
36+
print(node.value, end=" ")
37+
print_in_order(node.right)
38+
39+
# Main function to test the functionality
40+
def main():
41+
# Construct the binary tree
42+
root = TreeNode(0)
43+
root.left = TreeNode(1)
44+
root.right = TreeNode(2)
45+
root.left.left = TreeNode(3)
46+
root.left.left.right = TreeNode(5)
47+
root.right.right = TreeNode(4)
48+
root.right.right.left = TreeNode(6)
49+
root.right.right.right = TreeNode(7)
50+
51+
print("Original Binary Tree (InOrder):")
52+
print_in_order(root)
53+
print()
54+
55+
# Convert to full binary tree
56+
full_tree = convert_to_full_binary_tree(root)
57+
58+
print("Full Binary Tree (InOrder):")
59+
print_in_order(full_tree)
60+
print()
61+
62+
if __name__ == "__main__":
63+
main()

0 commit comments

Comments
 (0)