码迷,mamicode.com
首页 > 其他好文 > 详细

二叉搜索树与双向链表

时间:2019-03-18 01:44:57      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:null   traversal   node   http   rsa   public   搜索   stat   入栈   

public class Solution {
 public static TreeNode Convert(TreeNode pRootOfTree) {
        TreeNode p = pRootOfTree;
        TreeNode q = null;
        if(p!=null){
            LinkedList<TreeNode> list = new LinkedList<>();
            list.push(p);//根结点入栈
            while(p.left!=null){
                p = p.left;
                list.push(p);
            }
            TreeNode s = p;
            while(!list.isEmpty()){
                p = (TreeNode)list.pop();//最左结点
               
                if(q == null){
                    q = p;
                }
                else{
                    q.right = p;
                    p.left = q;
                    q = p;
                }
                if(p.right != null){
                    list.push(p.right);
                    p = p.right;
                    while(p.left!=null){
                        p = p.left;
                        list.push(p);
                    }
                }
            
            }
            return s;
        }
        return null;
    }
}

https://en.wikipedia.org/wiki/Tree_traversal#Iterative_Traversal

二叉搜索树与双向链表

标签:null   traversal   node   http   rsa   public   搜索   stat   入栈   

原文地址:https://www.cnblogs.com/dyq19/p/10549710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!