lintcode378- Convert Binary Search Tree to Doubly Linked Lis- medium

Convert a binary search tree to doubly linked list with in-order traversal.

Example

Given a binary search tree:

    4
   / 
  2   5
 / 
1   3

return 1<->2<->3<->4<->5

对每一个根节点来说,就是左边连上左子树的last(右下角),右边连上右子树的first(左下角)。然后返回自己的first(左下角)和last(右下角)。

用resultType来返回first last。

注意单侧null的细节处理。

1.自己实现

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 * Definition for Doubly-ListNode.
 * public class DoublyListNode {
 *     int val;
 *     DoublyListNode next, prev;
 *     DoublyListNode(int val) {
 *         this.val = val;
 *         this.next = this.prev = null;
 *     }
 * }
 */


public class Solution {
    /*
     * @param root: The root of tree
     * @return: the head of doubly list node
     */
     
    private class ResultType{
        public DoublyListNode first;
        public DoublyListNode last;
        public ResultType(DoublyListNode first, DoublyListNode last) {
            this.first = first;
            this.last = last;
        }
    }
    
    public DoublyListNode bstToDoublyList(TreeNode root) {
        // write your code here
        if (root == null) {
            return null;
        }
        ResultType result = helper(root);
        return result.first;
    }
    
    private ResultType helper(TreeNode root) {
        
        if (root == null) {
            return null;
        }
        
        DoublyListNode rootNode = new DoublyListNode(root.val);
        
        if (root.left == null && root.right == null) {
            return new ResultType(rootNode, rootNode);
        }
        
        ResultType left = helper(root.left);
        ResultType right = helper(root.right);
        
        if (left != null) {
            left.last.next = rootNode;
            rootNode.prev = left.last;
        }
        
        if (right != null) {
            rootNode.next = right.first;
            right.first.prev = rootNode;
        }
        
        DoublyListNode leftFirst = left == null ? rootNode : left.first;
        DoublyListNode rightLast = right == null ? rootNode : right.last;
        
        return new ResultType(leftFirst, rightLast);
    }
}

2. 九章实现:把 left == null 和left != null写到一个队里if clause里,更清楚,学习

但大体上这次我是自己写的答案和题解最接近的一次了!!!真的非常开心,而且一次AC。

class ResultType {
    DoublyListNode first, last;
    
    public ResultType(DoublyListNode first, DoublyListNode last) {
        this.first = first;
        this.last = last;
    }
}

public class Solution {
    /**
     * @param root: The root of tree
     * @return: the head of doubly list node
     */
    public DoublyListNode bstToDoublyList(TreeNode root) {  
        if (root == null) {
            return null;
        }
        
        ResultType result = helper(root);
        return result.first;
    }
    
    public ResultType helper(TreeNode root) {  
        if (root == null) {
            return null;
        }
        
        ResultType left = helper(root.left);
        ResultType right = helper(root.right);
        DoublyListNode node = new DoublyListNode(root.val);
        
        ResultType result = new ResultType(null, null);
        
        if (left == null) {
            result.first = node;
        } else {
            result.first = left.first;
            left.last.next = node;
            node.prev = left.last;
        }
        
        if (right == null) {
            result.last = node;
        } else {
            result.last = right.last;
            right.first.prev = node;
            node.next = right.first;
        }
        
        return result;
    }
}
原文地址:https://www.cnblogs.com/jasminemzy/p/7684861.html