关于树的刷题记录

1

题目描述:

检查子树。你有两棵非常大的二叉树:T1,有几万个节点;T2,有几万个节点。设计一个算法,判断 T2 是否为 T1 的子树。
如果 T1 有这么一个节点 n,其子树与 T2 一模一样,则 T2 为 T1 的子树,也就是说,从节点 n 处把树砍断,得到的树与 T2 完全相同。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/check-subtree-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool checkSubTree(TreeNode* t1, TreeNode* t2) {
        if(t2 == NULL) return true;
        if(t1 == NULL) return false;
        return helper(t1, t2) || checkSubTree(t1->left, t2) || checkSubTree(t1->right, t2);

    }

    bool helper(TreeNode* t1, TreeNode* t2) {
        if(t2==NULL) return true;
        if(t1==NULL) return false;
        if(t2->val != t1->val) return false;
        return helper(t1->left, t2->left) && helper(t1->right, t2->right);
    }
};
2

题目:

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

解:
2.1 递归解法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(!root) return 0;
        int depthLeft = maxDepth(root->left);
        int depthRight = maxDepth(root->right);
        int depth = depthLeft>depthLeft?depthLeft:depthRight;
        return depth + 1;
    }
};

2.2 基于栈的迭代DFS

// 答案参考:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/solution/di-gui-ji-die-dai-dfs-by-zerinhwang/
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(!root) return 0;
        stack<pair<TreeNode*, int>> s;

        int depth=0, max_depth=0;;
        while(1){
            // cout << root << endl;
            if(root){
                depth++;
                max_depth = max_depth>depth?max_depth:depth;
                cout << max_depth << endl;
                if(root->right) {
                    pair<TreeNode*, int> tmp(root->right, depth);
                    s.push(tmp);
                }
                root=root->left;

            }else if(s.empty()) break;
            else {
                root=s.top().first;
                depth = s.top().second;
                s.pop();
            }
        }
        return max_depth;
    }
};

2.3 基于迭代的BFS

//答案参考: https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/solution/di-gui-ji-die-dai-dfs-by-zerinhwang/
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(!root) return 0;
        int depth=0;
        queue<TreeNode*> q;
        q.push(root); q.push(nullptr);
        while(!q.empty()){
            root=q.front();
            if(root){
                if(root->left) q.push(root->left);
                if(root->right) q.push(root->right);
                q.pop();
            }else{
                depth++;
                if(q.size()==1) break;
                q.push(nullptr);
                q.pop();
            }
        }
        return depth;
    }
};

猜你喜欢

转载自www.cnblogs.com/JetBlock/p/12759931.html