首页 > 其他 > 详细

二叉搜索树与双向链表

时间:2019-03-17 23:38:55      阅读:206      评论:0      收藏:0      [点我收藏+]

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

二叉搜索树与双向链表

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!