手撕hot100之二叉树!看完这篇就AC~(一) - 5 小结
目录
1 题目
2 代码实现
c++
js
思考
题解
递归
迭代
3 题目
4 代码实现
c++
js
思考
题解
递归
迭代
5 小结
翻转二叉树
对称二叉树
1 题目
226. 翻转二叉树
给你一棵二叉树的根节点 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
2 代码实现
c++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) return root;
TreeNode* temp = root ->right ;
root ->right = root ->left ;
root ->left = temp ;
invertTree(root ->left);
invertTree(root ->right);
return root ;
}
};
js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if (root === null){
return root ;
}
let temp = root.right ;
root.right = root.left ;
root.left = temp ;
invertTree(root.left);
invertTree(root.right);
return root ;
};
思考
递归,左右交换。
题解
递归
var invertTree = function(root) {
if (!root) return null;
// 交换左右
[root.left, root.right] = [root.right, root.left];
invertTree(root.left);
invertTree(root.right);
return root;
};
迭代
BFS,用队列遍历每一个节点,遍历到就交换左右。
var invertTree = function(root) {
if (!root) return null;
const queue = [root];
while (queue.length) {
const cur = queue.shift();
// 交换
[cur.left, cur.right] = [cur.right, cur.left];
if (cur.left) queue.push(cur.left);
if (cur.right) queue.push(cur.right);
}
return root;
};
3 题目
101. 对称二叉树
给你一个二叉树的根节点 root , 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
提示:
树中节点数目在范围 [1, 1000] 内-100 <= Node.val <= 100
进阶:你可以运用递归和迭代两种方法解决这个问题吗?
4 代码实现
c++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
bool check (TreeNode* root1 , TreeNode* root2){
if (root1 == nullptr && root2 == nullptr) return true ;
if (root1 == nullptr || root2 == nullptr) return false ;
if (root1 ->val != root2 ->val) return false ;
return check(root1 ->left , root2 ->right) && check(root1 ->right , root2 ->left);
}
public:
bool isSymmetric(TreeNode* root) {
return check(root ->right , root ->left);
}
};
js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
const check = (root1 , root2) =>{
if (root1 === null && root2 === null ) return true ;
if (root1 === null || root2 === null ) return false ;
if (root1.val !== root2.val) return false;
return check(root1.left,root2.right) && check(root1.right,root2.left);
}
return check(root.left ,root.right);
};
思考
我第一反应就是递归,但是看了一下后面的进阶要求还有迭代的做法。
递归自己另外写一个check函数。
题解
递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x, left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
bool check(TreeNode* root1, TreeNode* root2) {
// 先判断 都为空 → 对称
if (root1 == nullptr && root2 == nullptr) return true;
// 再判断 一个空一个不空 → 不对称
if (root1 == nullptr || root2 == nullptr) return false;
// 值不相等 → 不对称
if (root1->val != root2->val) return false;
// 递归判断:左对右,右对左
return check(root1->left, root2->right) && check(root1->right, root2->left);
}
public:
bool isSymmetric(TreeNode* root) {
// 根节点为空,也是对称
if (!root) return true;
return check(root->left, root->right);
}
};
递归的时候要注意顺序
1. 两个都为空 → 对称 ✔️
2. 一个空一个不空 → 不对称 ❌
3. 值不同 → 不对称 ❌
4. 递归判断:左 ↔ 右,右 ↔ 左
迭代
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) return true; // 空树对称
// 用队列存储一对对需要比较的节点
queue q;
q.push(root->left);
q.push(root->right);
while (!q.empty()) {
// 每次取出一对节点
TreeNode* left = q.front(); q.pop();
TreeNode* right = q.front(); q.pop();
// 1. 都为空 → 对称,跳过
if (!left && !right) continue;
// 2. 一个空一个不空 → 不对称
if (!left || !right) return false;
// 3. 值不相等 → 不对称
if (left->val != right->val) return false;
// 关键:按对称顺序入队!
q.push(left->left); // 左孩子的左
q.push(right->right); // 右孩子的右
q.push(left->right); // 左孩子的右
q.push(right->left); // 右孩子的左
}
// 全部检查通过
return true;
}
};
左子树的左 ↔ 右子树的右
左子树的右 ↔ 右子树的左
5 小结
翻转二叉树
一个节点,交换左右,递归搞定
对称二叉树
两个节点,成对判断,左对右,右对左