226.翻转二叉树
https://leetcode.cn/problems/invert-binary-tree/description/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return null;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
invertTree(root.right);
return root;
}
}
98.验证二叉搜索树
https://leetcode.cn/problems/validate-binary-search-tree/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return check(root, -(1l << 31), (1l << 31) - 1);
}
private boolean check(TreeNode root, long rangeLeft, long rangeRight) {
if (root == null) return true;
if (root.val < rangeLeft || root.val > rangeRight) return false;
return check(root.left, rangeLeft, (long)root.val - 1) &&
check(root.right, (long)root.val + 1, rangeRight);
}
}
重叠子问题:翻转or验证左、右子树
当前层逻辑:翻转or验证大小关系
递归边界:叶子节点(无子树)
104.二叉树的最大深度
https://leetcode.cn/problems/maximum-depth-of-binary-tree/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
/**
* 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:
int maxDepth(TreeNode* root) {
ans = 0;
depth = 1;
calc(root);
return ans;
}
private:
void calc(TreeNode* root) {
if (root == nullptr) return ;
ans = max(ans, depth);
depth++;
calc(root->left);
calc(root->right);
depth--;
}
int depth;
int ans;
};
111.二叉树的最小深度
https://leetcode.cn/problems/minimum-depth-of-binary-tree/
class Solution {
public:
int minDepth(TreeNode *root) {
if (root == nullptr) {
return 0;
}
queue<pair<TreeNode *, int> > que;
que.emplace(root, 1);
while (!que.empty()) {
TreeNode *node = que.front().first;
int depth = que.front().second;
que.pop();
if (node->left == nullptr && node->right == nullptr) {
return depth;
}
if (node->left != nullptr) {
que.emplace(node->left, depth + 1);
}
if (node->right != nullptr) {
que.emplace(node->right, depth + 1);
}
}
return 0;
}
};
思路一(自底向上统计信息,分治思想)
最大深度=max(左子树最大深度,右子树最大深度)+1
思路二(自顶向下维护信息)
把“深度”作为一个全局变量——一个跟随结点移动而动态变化的信息递归一层,变量+1,在叶子处更新答案
这种写法需要注意保护和还原现场
推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习