好一点点就是好一点点嘛
RT
传入3个参数 <根节点,上次访问的节点,头结点>。Yahoo二面被问到!完跪....
1 void BST2DoubleList(TreeNode *root, TreeNode *& prev, TreeNode *& head) { 2 if (root == NULL) 3 return; 4 BST2DoubleList(root->left, prev, head); 5 root->left = prev; 6 if (prev != NULL) 7 prev->right = root; 8 else 9 head = root; 10 prev = root; 11 BST2DoubleList(root->right, prev, head); 12 } 13 14 TreeNode* treeToDoublyList(TreeNode *root) { 15 TreeNode *prev = NULL; 16 TreeNode *head = NULL; 17 BST2DoubleList(root, prev, head); 18 return head; 19 }
时间: 2024-10-10 14:25:09