文章

5

粉丝

0

获赞

28

访问

1.7k

头像
二叉树的深度 题解:
P4356
发布于2026年3月28日 22:40
阅读数 289

#include<bits/stdc++.h>
using namespace std;
struct Node
{
    char val;
    Node *left,*right;
    Node(char c):val(c),left(NULL),right(NULL){};
};

Node* build(string& s,int& x)
{
    if(x>=s.size() || s[x] == '#')
        return NULL;
    
    char curV = s[x];
    Node* node = new Node(curV);
    
    node->left = build(s,++x);
    node->right = build(s,++x);
    
    return node;
}


int getDepth(Node* node){
    if(node==NULL)
        return 0;
    return max(getDepth(node->left),getDepth(node->right)) + 1;
}
int main() {
    string s;
    while(cin>>s)
    {
       &n...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发