力扣--树题总结

1.传统二叉树 

112. 路径总和

class Solution {
public:
    bool flag = false;
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr) return 0;
        int pathsum=0;
        backtrack(root, targetSum, pathsum);
        return flag;
    }
    void backtrack(TreeNode* root, int targetSum, int pathsum){
        pathsum += root->val;
        if(root->left == nullptr && root->right == nullptr){
            if(pathsum == targetSum){
                flag = true;
            }
            return;
        }
        if(root->left){
            backtrack(root->left, targetSum, pathsum);
        }
        if(root->right){
            backtrack(root->right, targetSum, pathsum);
        }
    }
};

783. 二叉搜索树节点最小距离

/**
 * 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 minDiffInBST(TreeNode* root) {
        int ans = INT_MAX, pre = -1;
        dfs(root, pre, ans);
        return ans;
    }

    void dfs(TreeNode* root, int& pre, int& ans){
        //递归返回条件
        if(root == nullptr){
            return;
        }
        //前-一直递归到左下角
        dfs(root->left, pre, ans);
        //中-处理数据,第一次pre保存左下角值
        if(pre == -1){
            pre = root->val;
        }else{
            //递归退回到左下角父节点,ans取最小
            ans = min(ans, root->val - pre);
            //pre更新到当前节点的val
            pre = root->val;
        }
        //后续
        dfs(root->right, pre, ans);
    }
};

872. 叶子相似的树

/**
 * 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:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
        vector<int> num1;
        vector<int> num2;
        inorder(root1, num1);
        inorder(root2, num2);
        if(num1.size() != num2.size()) return false;
        for(int i=0; i< num1.size(); i++){
            if(num1[i] != num2[i]) return false;
        }
        return true;
    }

    void inorder(TreeNode* root, vector<int>& num){
        if(root==nullptr){
            return;
        }
        inorder(root->left, num);
        if(root->left==nullptr && root->right==nullptr){
            num.push_back(root->val);
        }
        inorder(root->right, num);
    }
};

897. 递增顺序搜索树

/**
 * 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* increasingBST(TreeNode* root) {
        vector<int> res;
        TreeNode* head = new TreeNode();
        TreeNode* p = head;
        dfs(root, res);
        for(int val: res){
            TreeNode* node = new TreeNode(val);
            p->right = node;
            p = p->right;
        }
        return head->right;
    }

    void dfs(TreeNode* root, vector<int>& res){
        if(root == nullptr){
            return;
        }
        dfs(root->left, res);
        res.push_back(root->val);
        dfs(root->right, res);
    }
};

100. 相同的树

/**
 * 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:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if (p == nullptr && q == nullptr) {
            return true;
        } else if (p == nullptr || q == nullptr) {
            return false;
        } else if (p->val != q->val) {
            return false;
        } else {
            return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        }
    }
};

  104. 二叉树的最大深度

 先序递归遍历树,取deep的最大值,deep是一个回溯的过程

/**
 * 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 maxdeep = 0;
    int maxDepth(TreeNode* root) {
        dfs(root, 0);
        return maxdeep;
    }

    void dfs(TreeNode* root, int deep){
        if(root == nullptr){
            return;
        }
        deep++;
        maxdeep = max(maxdeep, deep);
        dfs(root->left, deep);
        dfs(root->right, deep);
    }
};

更常见的写法

class Solution {
public:
    int maxdeep = 0;
    int maxDepth(TreeNode* root) {
        if(root == nullptr){
            return 0;
        }
        return max(maxDepth(root->left), maxDepth(root->right))+1;
    }

563. 二叉树的坡度

/**
 * 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 findTilt(TreeNode* root) {
        int ans = 0;
        dfs(root, ans);
        return ans;
    }

    int dfs(TreeNode* root, int& ans){
        if(root == nullptr){
            return 0;
        }
        int left = dfs(root->left, ans);
        int right = dfs(root->right, ans);
        ans += abs(left - right);
        return left+right+root->val;
    }
};

2.搜索树 

 101. 对称二叉树

 模仿上一题,不同处为:比较的是left->left, right->right和 left->right, right->left

/**
 * 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:
    bool isSymmetric(TreeNode* root) {
        return fun(root->left, root->right);
    }

    bool fun(TreeNode* left, TreeNode* right){
        if(left==nullptr && right==nullptr){
            return true;
        }else if(left==nullptr || right==nullptr){
            return false;
        }else if(left->val != right->val){
            return false;
        }else{
            return fun(left->left, right->right) && fun(left->right, right->left);
        }

    }
};

108. 将有序数组转换为二叉搜索树

每次以中间节点为根节点进行递归。 

/**
 * 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* sortedArrayToBST(vector<int>& nums) {
        return fun(nums, 0, nums.size()-1);
    }

    TreeNode* fun(vector<int>& nums, int left, int right){
        if(left > right){
            return nullptr;
        }
        int mid = (left + right)/2;
        TreeNode* node = new TreeNode(nums[mid]);
        node->left = fun(nums, left, mid-1);
        node->right = fun(nums, mid+1, right);
        return node;
    }
};

110. 平衡二叉树

 每个子树都是平衡二叉树,即当前树的左右高度<=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:
    bool isBalanced(TreeNode* root) {
        if(root == nullptr){
            return true;
        }
        return abs(maxDeepth(root->left) - maxDeepth(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
    }

    int maxDeepth(TreeNode* root){
        if(root == nullptr){
            return 0;
        }
        return max(maxDeepth(root->left), maxDeepth(root->right)) + 1;
    }
};

3.N叉树 

589. N 叉树的前序遍历

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<int> preorder(Node* root) {
        vector<int> ans;
        if(root == nullptr){
            return ans;
        }
        preorder(root, ans);
        return ans;
    }

    void preorder(Node* node, vector<int>& ans){
        if(node == nullptr){
            return;
        }
        ans.push_back(node->val);
        for(auto n : node->children){
            preorder(n, ans);
        }
    }
};

590. N 叉树的后序遍历

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<int> postorder(Node* root) {
        vector<int> ans;
        if(root == nullptr){
            return ans;
        }
        order(root, ans);
        return ans;
    }

    void order(Node* node, vector<int>& ans){
        if(node == nullptr){
            return;
        }
        for(auto n : node->children){
            order(n, ans);
        }
        ans.push_back(node->val);
    }
};

559. N 叉树的最大深度

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    int maxDepth(Node* root) {
        if(root == nullptr){
            return 0;
        }
        return dfs(root)+1;
    }

    int dfs(Node* root){
        if(root == nullptr){
            return 0;
        }
        int deep = 0;
        for(Node* n : root->children){
            deep = max(deep, dfs(n)+1);
        }
        return deep;
    }
};

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值