Topic Description:
Enter a binary search tree to convert the two-fork search tree into a sorted two-way list. You cannot create any new nodes, you can only adjust the point of the node pointer in the tree.
first, the idea of recursion
For the function treenode* convert (treenode* root), the first node of the two-fork tree that needs to be converted is returned, and the head node of the bidirectional linked list has been converted. From the root node, to the idea of recursion is to the left child and the right child to do black box handling, left = CONVERT (Root->left) is to return the root node of the tree has been transformed into a two-way linked list of the head node, right = Convert (root- >right) Return is the right child as the root node of the tree has been transformed into a two-way linked list of the head node, as long as left and right-hand are not empty, use the left-hand to find a two-way linked list of children's last node node, will node,root,right a connection, the problem solved.
steps:
1, root node for root, recursive root left child, left subtree constitute a two-way linked list, and return to the chain header node
2, using the left head node to traverse the last node of the double linked list
3, if the Zoozi tree linked table is not empty, link node and root
4, the same way to the right subtree form a two-way linked list, and return to the chain head node is
5. If the right subtree is not empty, link root
6, if left is not empty, return to the back, otherwise return root
The code is as follows:
/* struct TreeNode {int val;
struct TreeNode *left;
struct TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}};*/class Solution {public:treenode* Convert (treenode* ro
OT) {if (root = null) {return null;
} if (root->left = = NULL && Root->right = null) {return root;
}//1, root node is root, recursive root left child, the left subtree constitutes a two-way linked list, and return to the chain table head node treenode* left = Convert (Root->left);
treenode* node = left; 2. Use the left head node to traverse the last node of the two-linked list in the left-hand subtree node while (node!= null && node->right!= null) {node = node-&
Gt;right;
}//while//3, if the Zoozi tree list is not empty, connect node and root if (left!= NULL) {node->right = root;
root->left = node;
}//4, the same will be the right subtree into a two-way linked list, and return to the chain of the head of the end of the treenode* right-hand = Convert (root->right); 5. If the right subtree list is not empty, connect root and right-hand if (!= NULL) {root->right = Right;
Right->left = root;
//6, if left is not empty, returns to the back, otherwise returns the root return null?root:left = =; }
};
second, non-recursive thinking
The idea of non-recursion in sequence traversal in binary tree
Code:
struct treenode{int val;
Treenode* left;
treenode* right;
};
In contrast, non-recursive sequence traversal of binary tree void inorder (treenode* root) {//non-recursive version of the sequence traversal if (NULL = = root) {return;
} stack<treenode*> data;
treenode* node = root;
while (Node!=null | |!data.empty ()) {while (node!= NULL) {Data.push (node);
node = node->left;
}//while if (!data.empty ()) {node = Data.top ();
Data.pop ();
cout<<node->val<<endl;
node = node->right;
}}//while return;
} treenode* Convert (treenode* root) {if (null = = root) {return null;
} if (root->left = = NULL && Root->right = null) {return root;
} stack<treenode*> data;
treenode* node = root; treenode* pre = NULL;
The previous node, bool Isfirst = True, for storing the sequence traversal sequences; while (node!= null | |!data.empty ()) {while (node!= null) {Data.push (node);
node = node->left;
node = Data.top ();
Data.pop (); if (isfirst) {root = node;
The first node in the middle sequence traversal is recorded as the root pre = root;
Isfirst = false;
}else{pre->right = node;
Node->left = pre;
Pre = node;
node = node->right;
return root; }
New Ket Network topic Address:
https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&tqId=11179&rp=2&ru=/ta/ Coding-interviews&qru=/ta/coding-interviews/question-ranking