426. Convert Binary Search Tree to Sorted Doubly Linked List https://www.youtube.com/watch?v=FsxTX7-yhOw&t=1210s https://docs.google.com/document/d/1IIn5rXrUumqpxRrMKo76FbBx1ibTBDGso5rfENmkabw/edit class Solution { public Node treeToDoublyList(Node root) { Node head = null; Node prev = null; Stack<Node> stack = new Stack<>(); // Stack<Node> stack = new LinkedList<>(); why its not correct while(root != null || !stack.isEmpty()){ while(root != null){ stack.push(root); root = root.left; } root = stack.pop(); root.left = prev; if(prev != null){ prev.right = root; }else{ head = root; } Node right = root.right; head.left = root; root.right = head; prev = root; root = right; } return head; } }
原文地址:https://www.cnblogs.com/tobeabetterpig/p/9450664.html
时间: 2024-10-06 08:50:24