求取二叉数或者多叉树的深度

 一、前序遍历回溯法

1.1总体思路

        deepth记录本节点到根节点的路径长度,result全局变量更新max(每个孩子的到根节点的长度)。(也可以解决某一孩子到根节点高度问题,添加一个if判断即可)。

        如有孩子则deepth++,递归函数(孩子,deepth),deepth--

1.2代码实现

1.2.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 result;
    void getDepth(TreeNode* node, int deepth){
        if(!node) return;

        // 中, 记录每一个节点到根节点的高度, 根节点初始高度为1
        result = max(result,deepth);

        if(node->left){ // 左
            deepth++;
            getDepth(node->left,deepth);
            deepth--;
        }
        if(node->right){ // 右
            deepth++;
            getDepth(node->right,deepth);
            deepth--;
        }
    }
    int maxDepth(TreeNode* root) {
        // 使用前序回溯遍历获取最大深度
        result = 0;
        if(!root) return result;
        getDepth(root,1);
        return result;
    }
};

1.2.2多叉树

/*
// 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 result;// 全局变量统计最大值
    void getDepth(TreeNode* node, int deepth){// deepth 为当前节点到根节点的路径长度,root到根节点长度初始为1
        if(!node) return;

        // 中, 记录每一个节点到根节点的高度, 根节点初始高度为1
        result = max(result,deepth);

        for(int i=0; i< node->children.size(); i++){
            deepth++;
            getDepth(node->children[i],deepth];
            deepth--;
        }
    }
    int maxDepth(TreeNode* root) {
        // 使用前序回溯遍历获取最大深度
        result = 0;
        if(!root) return result;
        getDepth(root,1);
        return result;
    }
};

二、后续遍历迭代法

2.1总体思路

        先计算出孩子的深度,再计算该节点的深度为 1 + max(孩子的深度)

2.2代码实现

2.2.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 getDepth(TreeNode* node){
        if(!node) return 0;

        int leftDepth = getDepth(node->left);
        int rightDepth = getDepth(node->right);
        
        return 1+max(leftDepth, rightDepth);
    }
    int maxDepth(TreeNode* root) {
        return getDepth(root);
    }
};

2.2.2多叉树

/*
// 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 getDepth(Node*node){
        // 后续遍历求取孩子节点的深度后,再max()+1
        if(!node) return 0;
        
        int temp = 0; // 记录孩子的最大深度
        for(int i = 0; i < node->children.size(); i++){
            temp = max( getDepth(node->children[i]), temp);
        }

        return 1 + temp;

    }
    
    int maxDepth(Node* root) {
        return getDepth(root);
    }
};

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值