Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def inOrderTraversal(node): if node is None: return inOrderTraversal(node.left) print(node.data, end=", ") inOrderTraversal(node.right) root = TreeNode(13) node7 = TreeNode(7) node15 = TreeNode(15) node3 = TreeNode(3) node8 = TreeNode(8) node14 = TreeNode(14) node19 = TreeNode(19) node18 = TreeNode(18) root.left = node7 root.right = node15 node7.left = node3 node7.right = node8 node15.left = node14 node15.right = node19 node19.left = node18 # Traverse inOrderTraversal(root) #Python
#include
#include
typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* newTreeNode(int data) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->data = data; node->left = NULL; node->right = NULL; return node; } void inOrderTraversal(TreeNode* node) { if (node == NULL) return; inOrderTraversal(node->left); printf("%d, ", node->data); inOrderTraversal(node->right); } int main() { TreeNode* root = newTreeNode(13); root->left = newTreeNode(7); root->right = newTreeNode(15); root->left->left = newTreeNode(3); root->left->right = newTreeNode(8); root->right->left = newTreeNode(14); root->right->right = newTreeNode(19); root->right->right->left = newTreeNode(18); inOrderTraversal(root); return 0; } //C
public class Main { public static class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { this.data = data; left = null; right = null; } } public static void inOrderTraversal(TreeNode node) { if (node == null) return; inOrderTraversal(node.left); System.out.print(node.data + ", "); inOrderTraversal(node.right); } public static void main(String[] args) { TreeNode root = new TreeNode(13); root.left = new TreeNode(7); root.right = new TreeNode(15); root.left.left = new TreeNode(3); root.left.right = new TreeNode(8); root.right.left = new TreeNode(14); root.right.right = new TreeNode(19); root.right.right.left = new TreeNode(18); inOrderTraversal(root); } } // Java
Python result:
C result:
Java result:
3, 7, 8, 13, 14, 15, 18, 19,
3, 7, 8, 13, 14, 15, 18, 19,
3, 7, 8, 13, 14, 15, 18, 19,