二叉搜索樹與雙向鏈表

題目
輸入一棵二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。要求不能創建任何新的結點,只能調整樹中結點指針的指向。

分析
二叉搜索樹轉變為排序的雙向鏈表,即將二叉搜索樹按照中序遍歷。中序遍歷的結果即是排好序的結果,將中序遍歷時的每一個節點的left和right進行調整便可實現排好序的雙向鏈表

代碼

import java.util.Stack;

public class Solution {

    public static void main(String[] args) {
        // 創建根節點
        TreeNode root = new TreeNode(6); 
        Solution solution = new Solution();
        int[] a = { 4, 7, 2, 5};
        // 插入節點
        for (int i = 0; i < a.length; i++) { 
            solution.initTree(root, a[i]);
        }
        
        solution.inFindDiGui(root);
        System.out.println();
        solution.inFindNoDiGui(root);
        System.out.println();
        TreeNode head = solution.Convert(root);
        TreeNode node = head;
        while(node != null){
            System.out.print(node.val+"->");
            node = node.right;
        }

    }
    
    // 將二叉搜索樹轉換為排序的雙向鏈表
    public TreeNode Convert(TreeNode pRootOfTree) {
        TreeNode p = pRootOfTree;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        boolean flag = true;
        TreeNode head = null;
        TreeNode pre = null;
        while(p!=null || !stack.isEmpty()){
            while(p!=null){
                stack.push(p);
                p = p.left;
            }
            if(!stack.isEmpty()){
                p = stack.pop();
                // 處理頭節點
                if(flag){
                    p.left = null;
                    head = p;
                    pre = p;
                    flag = false;
                } else {
                    pre.right = p;
                    p.left = pre;
                    pre = pre.right;
                }
                p = p.right;
            }
        }
        return head;
    }
    
    // 遞歸方式中序遍歷二叉搜索樹
    public void inFindDiGui(TreeNode root) {
        TreeNode p = root;
        if (null != p) {
            inFindDiGui(p.left);
            System.out.print(p.val + "->");
            inFindDiGui(p.right);
        }
    }
    
    // 非遞歸方式中序遍歷二叉搜索樹
    public void inFindNoDiGui(TreeNode root) {
        TreeNode p = root;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while(p!=null || !stack.isEmpty()){
            while(p!=null){
                stack.push(p);
                p = p.left;
            }
            if(!stack.isEmpty()){
                p = stack.pop();
                System.out.print(p.val + "->");
                p = p.right;
            }
        }
        
    }
    
    // 初始化二叉搜索樹
    public void initTree(TreeNode root, int val) {
        if (root != null) {
            if (val < root.val) {
                if (root.left == null) {
                    root.left = new TreeNode(val);
                } else {
                    initTree(root.left, val);
                }
            } else {
                if (root.right == null) {
                    root.right = new TreeNode(val);
                } else {
                    initTree(root.right, val);
                }
            }
        }
    }

}

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

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

    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容