题解 | #二叉树的中序遍历#

二叉树的中序遍历

https://www.nowcoder.com/practice/0bf071c135e64ee2a027783b80bf781d?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295%26fromPut%3Dpc_kol_aaaxiu

先想到利用递归,然后使用js的concat方法解题

function midOrder(root){

    if(!root) return []

    if(!root.left&&!root.right) return [].concat(root.val)

    else  return [].concat(midOrder(root.left)).concat(root.val).concat(midOrder(root.right)

}

以上是中序遍历的顺序结果,前序,后序遍历原理相同,调换其中concat的顺序就行

前序,中序,后序遍历其实就是根节点什么时候访问的区别,例如:前序就是根节点先访问

#js#
全部评论

相关推荐

ResourceUtilization:算法很难了,现在都需要相关论文还有对应的实习,可以先试试中厂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务