携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第25天,点击查看活动详情
题目(Invert Binary Tree)
链接:https://leetcode-cn.com/problems/invert-binary-tree
解决数:3403
通过率:79.3%
标签:树 深度优先搜索 广度优先搜索 二叉树
相关公司:google amazon bytedance
给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
示例 1:
输入: root = [4,2,7,1,3,6,9]
输出: [4,7,2,9,6,3,1]
示例 2:
输入: root = [2,1,3]
输出: [2,3,1]
示例 3:
输入: root = []
输出: []
提示:
- 树中节点数目范围在
[0, 100]内 -100 <= Node.val <= 100
思路:递归
- 递归交换当前节点的左右节点,当节点为null时返回
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if(root === null) {
return null;
}
let right = invertTree(root.right);
let left = invertTree(root.left);
root.left = right;
root.right = left;
return root;
};
- 或者这样写亦可
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if(root === null) {
return null;
}
[root.left,root.right] = [invertTree(root.right),invertTree(root.left)];
return root;
};
思路2:DFS 栈
- 深度优先遍历
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
let stack = [root];
while(stack.length > 0){
let cur = stack.pop();
if(cur === null) continue;
[cur.left,cur.right] = [cur.right,cur.left];
stack.push(cur.right);
stack.push(cur.left);
}
return root;
};
思路3:BFS 队列
- 广度优先遍历
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
let queue = [root];
while(queue.length > 0){
let cur = queue.pop();
if(cur === null) continue;
[cur.left,cur.right] = [cur.right,cur.left];
queue.unshift(cur.left);
queue.unshift(cur.right);
}
return root;
};
思路4:前序遍历
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if(root === null) return null;
[root.left,root.right] = [root.right,root.left];
invertTree(root.left);
invertTree(root.right);
return root;
};
思路5:中序遍历
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if(root === null) return null;
invertTree(root.left);
[root.left,root.right] = [root.right,root.left];
// 此时的root.left 是上一步的 root.right
invertTree(root.left);
return root;
};
思路6:后序遍历
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if(root === null) return null;
invertTree(root.left);
invertTree(root.right);
[root.left,root.right] = [root.right,root.left];
return root;
};