Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
binary_tree_array = ['R', 'A', 'B', 'C', 'D', 'E', 'F', None, None, None, None, None, None, 'G'] def left_child_index(index): return 2 * index + 1 def right_child_index(index): return 2 * index + 2 def pre_order(index): if index >= len(binary_tree_array) or binary_tree_array[index] is None: return [] return [binary_tree_array[index]] + pre_order(left_child_index(index)) + pre_order(right_child_index(index)) def in_order(index): if index >= len(binary_tree_array) or binary_tree_array[index] is None: return [] return in_order(left_child_index(index)) + [binary_tree_array[index]] + in_order(right_child_index(index)) def post_order(index): if index >= len(binary_tree_array) or binary_tree_array[index] is None: return [] return post_order(left_child_index(index)) + post_order(right_child_index(index)) + [binary_tree_array[index]] print("Pre-order Traversal:", pre_order(0)) print("In-order Traversal:", in_order(0)) print("Post-order Traversal:", post_order(0)) #Python
#include
#include
typedef struct TreeNode { char data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* createNewNode(char data) { TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } void postOrderTraversal(TreeNode* root) { if (root == NULL) { return; } postOrderTraversal(root->left); postOrderTraversal(root->right); printf("%c, ", root->data); } int main() { TreeNode* root = createNewNode('R'); TreeNode* nodeA = createNewNode('A'); TreeNode* nodeB = createNewNode('B'); TreeNode* nodeC = createNewNode('C'); TreeNode* nodeD = createNewNode('D'); TreeNode* nodeE = createNewNode('E'); TreeNode* nodeF = createNewNode('F'); TreeNode* nodeG = createNewNode('G'); root->left = nodeA; root->right = nodeB; nodeA->left = nodeC; nodeA->right = nodeD; nodeB->left = nodeE; nodeB->right = nodeF; nodeF->left = nodeG; // Traverse postOrderTraversal(root); free(nodeG); free(nodeF); free(nodeE); free(nodeB); free(nodeC); free(nodeD); free(nodeA); free(root); return 0; } //C
public class Main { public static class TreeNode { String data; TreeNode left; TreeNode right; public TreeNode(String data) { this.data = data; this.left = null; this.right = null; } } public static void postOrderTraversal(TreeNode node) { if (node == null) { return; } postOrderTraversal(node.left); postOrderTraversal(node.right); System.out.print(node.data + ", "); } public static void main(String[] args) { TreeNode root = new TreeNode("R"); TreeNode nodeA = new TreeNode("A"); TreeNode nodeB = new TreeNode("B"); TreeNode nodeC = new TreeNode("C"); TreeNode nodeD = new TreeNode("D"); TreeNode nodeE = new TreeNode("E"); TreeNode nodeF = new TreeNode("F"); TreeNode nodeG = new TreeNode("G"); root.left = nodeA; root.right = nodeB; nodeA.left = nodeC; nodeA.right = nodeD; nodeB.left = nodeE; nodeB.right = nodeF; nodeF.left = nodeG; // Traverse postOrderTraversal(root); } } // Java
Python result:
C result:
Java result:
C, D, A, E, G, F, B, R,
C, D, A, E, G, F, B, R,
C, D, A, E, G, F, B, R,