• 37二叉树的深度+变式题输出最长路径上的所有节点


    题目描述

    输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
     
     
    思路:
    1)递归方法:记住每次递归返回的是啥,这里返回的是该根节点对应的高度。
    所以先找左节点的高度,和右节点的高度,然后两者的最大高度加上1就是答案。
    /*
    struct TreeNode {
        int val;
        struct TreeNode *left;
        struct TreeNode *right;
        TreeNode(int x) :
                val(x), left(NULL), right(NULL) {
        }
    };*/
    class Solution {
    public:
        int TreeDepth(TreeNode* pRoot){
            if(pRoot == nullptr){
                return 0;
            }
            int left = TreeDepth(pRoot -> left);
            int right = TreeDepth(pRoot -> right);
            return max(left,right) + 1;
        }
    };

    2)循环版本

    循环就是二叉树的层次遍历,使用队列queue,循环看有多少层,那么深度就是多少。

    /*
    struct TreeNode {
        int val;
        struct TreeNode *left;
        struct TreeNode *right;
        TreeNode(int x) :
                val(x), left(NULL), right(NULL) {
        }
    };*/
    class Solution {
    public:
        int TreeDepth(TreeNode* pRoot){
            if(pRoot == nullptr){
                return 0;
            }
            int high = 0;
            queue<TreeNode*> q;
            q.push(pRoot);
            while(q.size() != 0){
                int sz = q.size();
                while(sz--){
                    TreeNode* tmpnode = q.front();
                    q.pop();
                    if(tmpnode -> left != nullptr){
                        q.push(tmpnode -> left);
                    }
                    if(tmpnode -> right != nullptr){
                        q.push(tmpnode -> right);
                    }
                }
                ++high;
            }
            return high;
        }
    };

     变式题:给一个二叉树,找出其中最长的距离并输出路径

     方法是利用上面求最大深度的函数,每次在当前节点比较左右子树的最大深度,遍历的时候往最大深度的那边走。

    http://www.cnblogs.com/wxdjss/p/5471799.html

    #include<iostream>
    #include<vector>
    
    using namespace std;
    
    typedef struct node
    {
    char data;//结点数据
    struct node *lchild,*rchild;//二叉树结点类型
    }BSTree;//二叉树结点类型
    
    vector<char> path;
    
    void Createb(BSTree **p)//建立二叉树
    {
        char ch;
        cin>>ch;
        if(ch!='.')
        {
            *p=(BSTree*)malloc(sizeof(BSTree));//申请空间
            (*p)->data=ch;//空间赋值
            Createb(&(*p)->lchild);//生成左子树
            Createb(&(*p)->rchild);//生成右子树
        }
        else *p=NULL;//空结点
    }
    
    int TreeDepth(BSTree *root)//树的深度
    {
        if(root==NULL)
            return 0;
        int left=TreeDepth(root->lchild);
        int right=TreeDepth(root->rchild);
        return (left>right) ? (left+1) : (right+1);
    }
    
    void Findlongestpath(BSTree *root)//找到第一条最长路径
    {
        path.push_back(root->data);
        if(root->lchild==NULL&&root->lchild==NULL)
            return;
        if(TreeDepth(root->lchild)>=TreeDepth(root->rchild))//如果左子树高度大于或右子树高度
        Findlongestpath(root->lchild);//遍历左子树继续查找
        else Findlongestpath(root->rchild);//遍历右子树进行查找
    }
    
    void printpath()
    {
        vector<char>::iterator ite;
        for(ite=path.begin();ite!=path.end();++ite)
            cout<<*ite<<" ";
            cout<<endl;
            path.clear();//清理容器
    }
            
    void main()
    {
        BSTree *root;//二叉树根结点
        cout<<"create BSTree root:"<<endl;//创建二叉树
        Createb(&root);
        cout<<"root's depth is"<<TreeDepth(root)<<endl;//二叉树深度
        Findlongestpath(root);//找到最长路径
        cout<<"the longest path is:"<<endl;
        printpath();//打印
    }
  • 相关阅读:
    洛谷 U140360 购物清单
    洛谷 U140359 批量处理
    洛谷 U140358 操作系统
    洛谷U140357 Seaway连续
    洛谷 U141394 智
    洛谷 U141387 金
    CF1327F AND Segments
    刷题心得—连续位运算题目的小技巧
    CF743C Vladik and fractions
    洛谷 P6327 区间加区间sin和
  • 原文地址:https://www.cnblogs.com/dingxiaoqiang/p/8195901.html
Copyright © 2020-2023  润新知