递归题目树型实战

简介: 递归题目树型实战


实战

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等技术内容,立即学习

相关文章
|
5月前
|
存储 C++ 容器
【C++&数据结构】二叉树(结合C++)的经典oj例题 [ 盘点&全面解析 ](24)
【C++&数据结构】二叉树(结合C++)的经典oj例题 [ 盘点&全面解析 ](24)
|
5月前
|
存储 算法
力扣102、 二叉树层级遍历
力扣102、 二叉树层级遍历
|
6月前
|
算法
代码随想录算法训练营第十七天 | LeetCode 110. 平衡二叉树、257. 二叉树的所有路径、404. 左叶子之和
代码随想录算法训练营第十七天 | LeetCode 110. 平衡二叉树、257. 二叉树的所有路径、404. 左叶子之和
30 0
|
7月前
|
存储
【二叉树】——链式结构(快速掌握递归与刷题技巧)
【二叉树】——链式结构(快速掌握递归与刷题技巧)
36 0
|
10月前
|
Sentinel
力扣19删除链表的倒数第 N 个结点:思路分析+图文全解+方法总结(快慢指针法&递归法)+深入思考
力扣19删除链表的倒数第 N 个结点:思路分析+图文全解+方法总结(快慢指针法&递归法)+深入思考
115 0
|
12月前
|
存储 移动开发 算法
树和二叉树知识点总结
树和二叉树知识点总结
11631 0
树的引子, 顺序查找和二分查找
树的引子, 顺序查找和二分查找
|
机器学习/深度学习
LeetCode每日一题(13)——建立四叉树(递归)
建立四叉树 1.题目 2.示例 3.思路 4.代码
111 0
LeetCode每日一题(13)——建立四叉树(递归)
递归的思路
今天给老铁们回顾一下递归的思路以及方法,也是给自己的一个归纳总结。
92 0
递归的思路
|
算法
数据结构与算法题目集(中文) - 7-17 汉诺塔的非递归实现(25 分)(附:递归版)
数据结构与算法题目集(中文) - 7-17 汉诺塔的非递归实现(25 分)(附:递归版)
132 0