运行 ❯
获取您
的
网站
×
更改方向
更改主题,深色/浅色
转到 Spaces
Python
C
Java
class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def postOrderTraversal(node): if node is None: return postOrderTraversal(node.left) postOrderTraversal(node.right) print(node.data, end=", ") root = TreeNode('R') nodeA = TreeNode('A') nodeB = TreeNode('B') nodeC = TreeNode('C') nodeD = TreeNode('D') nodeE = TreeNode('E') nodeF = TreeNode('F') nodeG = 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) #Python
#include <stdio.h> #include <stdlib.h> 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 结果
C 结果
Java 结果
C、D、A、E、G、F、B、R
C、D、A、E、G、F、B、R
C、D、A、E、G、F、B、R