Binary search tree becomes doubly linked list

Title Description
Enter a binary search tree and convert the binary search tree into a sorted doubly linked list. It is required that no new node can be created, and only the pointer of the node pointer in the tree can be adjusted.
Analysis: can be traversed in order

在这里插入代码片
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
   TreeNode head=null,t=null;
	 public TreeNode Convert(TreeNode p) {
	      if(p==null)return null;
	      Convert(p.left);
	      if(head==null) 
	    	  t=head=p;
	      else
	      {
	    	  t.right=p;
	    	  p.left=t;
	    	  t=t.right;
	      }
	      Convert(p.right);
	      return head;
	    }
}
Published 167 original articles · Like 16 · Visits 30,000+

Guess you like

Origin blog.csdn.net/feiqipengcheng/article/details/105469841
Recommended