0
点赞
收藏
分享

微信扫一扫

js 反转二叉树

七千22 2022-02-14 阅读 60

什么是反转二叉树

初始数据


let list = {
  id: "4",
  left: {
    id: "2",
    left: {
      id: "1",
      left: null,
      right: null,
    },
    right: {
      id: "3",
      left: null,
      right: null,
    },
  },
  right: {
    id: "7",
    left: {
      id: "6",
      left: null,
      right: null,
    },
    right: {
      id: "9",
      left: null,
      right: null,
    },
  },
};

反转二叉树函数

function reverseBTree(node) {
  if (!node) {
    return;
  }
  let tmp = node.left;
  node.left = node.right;
  node.right = tmp;
  reverseBTree(node.left);
  reverseBTree(node.right);
}
举报

相关推荐

0 条评论