二叉树中序遍历
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Convert(pRootOfTree)
{
let head = null; //指向链表首节点
let pre = null; //指向遍历节点的上一节点
const dfs = (root) => {
if(!root) return;
dfs(root.left);
root.left = pre;
if(!pre){
head = root; //root是最小节点
}else{
pre.right = root;
}
pre = root;
dfs(root.right);
}
dfs(pRootOfTree);
return head;
}
module.exports = {
Convert : Convert
};


京公网安备 11010502036488号