• LintCode 将二叉查找树转换成双链表


    /**
     * 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 static DoublyListNode nodeList=null;
        public DoublyListNode bstToDoublyList(TreeNode root) {  
            // Write your code here
            if(root==null)
                return null;
            recursion(root);
            
            return nodeList;
        }
        public void recursion(TreeNode root)
        {
            if(root==null)
                return;
            recursion(root.right);//因为nodeList最后是移动末节点,所以从右边开始递归,这样nodeList就会变成最初的节点
            DoublyListNode node=new DoublyListNode(root.val);
            
            node.next=nodeList;
            if(nodeList!=null){
                nodeList.prev=node;
            }
            nodeList=node;
            recursion(root.left);
        }
    }
    
  • 相关阅读:
    垃圾收集器
    垃圾收集算法
    动态绑定
    数据库连接池原理
    分布式事务:两段式提交(最终一致性)
    C# 推箱子游戏&对战游戏
    C# 结构体和类的区别
    C# 类&结构体&枚举
    C# 哈希表&列队&栈
    C# 数组&集合&泛型集合
  • 原文地址:https://www.cnblogs.com/temporary/p/7645984.html
Copyright © 2020-2023  润新知