day30

【0104.二叉树的最大深度】

/**
 * 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) {
        int result = 0;
        queue<TreeNode*> que;
        if (root != NULL)   que.push(root);
        int depth = 0;
        while(!que.empty()) {
            int size = que.size();  
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left)     que.push(node->left);
                if (node->right)    que.push(node->right);
            }
        }
        return depth;
    }
};
  • 迭代法之前写过 因为有模板 所以比较容易想到

  • 递归法 说实话 关于三部曲中的第一部“返回值我知道 但输入参数如何确定”我不太能get到 看了思路的两点提示 1. 输入参数是根节点 2.终止条件是访问到叶子节点 那么至于三部曲中的第三部 简单循环 这点我能想到 就是左子树右子树各自也求其深度

    没想到 以下就运行成功了

/**
 * 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 depth (TreeNode* root) {
        int result = 1;
        if (root->left == NULL && root->right == NULL)  return result;
        if (root->left != NULL ){
            int tempResult = depth(root->left) + 1;
            result = result > tempResult ? result : tempResult;
        }
        if (root->right != NULL ){
            int tempResult = depth(root->right) + 1;
            result = result > tempResult ? result : tempResult;
        }
        return result;
    }
    int maxDepth(TreeNode* root) {
        if (root == NULL)   return 0;
        int result = depth (root);     
        return result;
    }
};
  • 以下 一个是迭代法标准代码----比我的简洁 一个是回溯法求深度----没理解真谛
class solution {
public:
    int getdepth(treenode* node) {
        if (node == NULL) return 0;
        int leftdepth = getdepth(node->left);       // 左
        int rightdepth = getdepth(node->right);     // 右
        int depth = 1 + max(leftdepth, rightdepth); // 中
        return depth;
    }
    int maxdepth(treenode* root) {
        return getdepth(root);
    }
};
class solution {
public:
    int result;
    void getdepth(treenode* node, int depth) {
        result = depth > result ? depth : result; // 中

        if (node->left == NULL && node->right == NULL) return ;

        if (node->left) { // 左
            depth++;    // 深度+1
            getdepth(node->left, depth);
            depth--;    // 回溯,深度-1
        }
        if (node->right) { // 右
            depth++;    // 深度+1
            getdepth(node->right, depth);
            depth--;    // 回溯,深度-1
        }
        return ;
    }
    int maxdepth(treenode* root) {
        result = 0;
        if (root == NULL) return result;
        getdepth(root, 1);
        return result;
    }
};
posted @ 2022-12-03 21:15  跬步瑶  阅读(20)  评论(0)    收藏  举报