大前端 2017-03-09
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function Convert(pRootOfTree) { if (!pRootOfTree) { return null; } var lastNode = null; lastNode = ConvertNode(pRootOfTree, lastNode); var head = lastNode; while (head && head.left) { head = head.left; } return head; } function ConvertNode(node, lastNode) { if (!node) { return; } if (node.left) { lastNode = ConvertNode(node.left, lastNode); } node.left = lastNode; if (lastNode) { lastNode.right = node; } lastNode = node; if (node.right) { lastNode = ConvertNode(node.right, lastNode); } return lastNode; }