本文主要是介绍剑指offer-70-36 二叉搜索树转双向链表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&tqId=11179&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
想明白过程就很简单,记录上一次访问的点。
/*
struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};*/
class Solution {
public:void dfs(TreeNode* root, TreeNode* &last, TreeNode* &head) {if (!root) return;if (root->left) {dfs(root->left, last, head);}if (head == NULL) {head = root;} else {last->right = root;root->left = last;}last = root;if (root->right) {dfs(root->right, last, head);}}TreeNode* Convert(TreeNode* pRootOfTree){if (pRootOfTree == NULL) return NULL;TreeNode* last = NULL, *head = NULL;dfs(pRootOfTree, last, head);return head;}
};
这篇关于剑指offer-70-36 二叉搜索树转双向链表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!