zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    
  • 相关阅读:
    vmareworkstation 15 安装密钥
    Linux进入ftp界面退出方法
    linux安装mysql(5.1.73)
    安装http服务,用http搭建web网
    telnet远程连接
    yum出现问题解决方法
    samba
    nfs搭建
    解决VMwareworkstation无法在windows上运行
    2 shell编程
  • 原文地址:https://www.cnblogs.com/temporary/p/7645984.html
Copyright © 2011-2022 走看看