- Problem-solving ideas:
- 1. The left subtree of a double chain structure, and returns the list head node.
- 2. Navigate to the left subtree doubly linked list last node.
- 3. If the left subtree list is not empty, it will be appended to the current root left subtree list.
- 4. The dual configuration list right subtree, and returns a list head node.
- 5. If the right subtree after the list is not empty, the list is added to the root node.
- 6. Determine whether the node returned by the left subtree list is empty.
public class o_26 {
public TreeNode Convert(TreeNode pRootOfTree) {
if (pRootOfTree == null)
return null;
if (pRootOfTree.left == null && pRootOfTree.right == null)
return pRootOfTree;
TreeNode left = Convert(pRootOfTree.left);
TreeNode p = left;
while (p != null && p.right != null)
p = p.right;
if (left != null) {
p.right = pRootOfTree;
pRootOfTree.left = p;
}
TreeNode right = Convert(pRootOfTree.right);
if (right != null) {
right.left = pRootOfTree;
pRootOfTree.right = right;
}
return left != null ? left : pRootOfTree;
}
}