/** * 注意:left/right值若没有显示设置为null,值即为undefined * 在调用二叉树前、中、后序遍历方法时,由于参数设置了默认值(tree) * 所以进入了死循环 */
const tree = { value: 5, left: { value: 3, left: { value: 2, left: null, right: null }, right: { value: 4, left: null, right: null } }, right: { value: 7, left: { value: 6, left: null, right: null }, right: { value: 8, left: null, right: { value: 10, left: null, right: null } } } }
递归:分而治之思想
const reverseTree = (node = tree) => { if(node === null) return null return { value: node.value, left: reverseTree(node.right), right: reverseTree(node.left), } }
标签:node,right,翻转,value,二叉树,null,left From: https://www.cnblogs.com/zhenjianyu/p/17070554.html