Wins the offer-32. Binary search tree with a doubly linked list (191)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_38332722/article/details/100558114

32. The binary search trees and doubly-linked list (191)

  • Description Title: input a binary search tree, converts the binary search tree into a doubly linked list sorted. Requirements can not create any new node, point to only adjust the tree node pointer.

  • Ideas: The nodes in preorder tree. For the current node, we need to change the current node leftpoints to its previous nodes, so that before a node rightpoints to the current node.

  • Code:

    package _32.二叉搜索树与双向链表;
    
    import java.util.Stack;
    
    /**
     * 题目描述:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
     * 要求不能创建任何新的结点,只能调整树中结点指针的指向。
     * @author Administrator
     *
     */
    public class ConvertBSTtoLink {
    	/**
    	 * 采用中序遍历结点:对于每一个结点,需要更改前一个结点的right(后继结点),
    	 * 和当前结点的left(前驱结点)
    	 * @param pRootOfTree
    	 * @return
    	 */
        public static TreeNode Convert(TreeNode pRootOfTree) {
           if(pRootOfTree == null){
        	   return null;
           }
           
           Stack<TreeNode> stack = new Stack<>();
           //保存当前结点的前一个结点,以便给前一结点的 right赋值
           TreeNode pre = null;
           //中序遍历结点
           TreeNode cur = pRootOfTree;
           //保存链表的头结点
           TreeNode linkedHead = null;
           
           boolean firstFlag = true;
          
           while(!stack.isEmpty() || cur != null){
        	   while(cur != null){
        		   stack.push(cur);
        		   cur = cur.left;
        	   }
        	   if(cur == null){
        		   cur = stack.pop();
        		   if(firstFlag){
        			   linkedHead = cur;
        			   pre = cur;
        			   firstFlag = false;
        		   }
        		   else{
        			   pre.right = cur;
        			   cur.left = pre;
        			   pre = cur;
        		   }
        		   cur = cur.right;
        	   }
           }
           return linkedHead;
        }
        
        public static void main(String[] args) {
        	  //          10
            //          /   \
            //         6     14
            //       /  \   / \
            //      4    8 12  16
            TreeNode root = new TreeNode(10);
            root.left = new TreeNode(6);
            root.right = new TreeNode(14);
            root.left.left = new TreeNode(4);
            root.left.right = new TreeNode(8);
            root.right.left = new TreeNode(12);
            root.right.right = new TreeNode(16);
            TreeNode result = Convert(root);
            while(result != null){
            	 System.out.println(result);
            	 result = result.right;
            }
    	}
    }
    class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
        }
    
    	@Override
    	public String toString() {
    		return "TreeNode [val=" + val + "]";
    	}
    }
    

Guess you like

Origin blog.csdn.net/qq_38332722/article/details/100558114