Leetcode:maximum_depth_of_binary_tree題解,

來源:互聯網
上載者:User

Leetcode:maximum_depth_of_binary_tree題解,

一、     題目

   給定一個二叉樹,求它的最大深度。最大深度是沿從根節點,到分葉節點最長的路徑。

二、     分析

   (做到這裡發現接連幾道題都是用遞迴,可能就是因為自己時挑的簡單的做的吧。)

   找出最深路徑,則每經過一個節點要加上1,當遇到空節點時,返回0。

   在網上看了看有的做法除了麻煩點但是很不錯的方法,比如使用棧和隊列等


/** * Definition for binary tree * 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==NULL)         return 0;        return max(maxDepth(root->left),maxDepth(root->right))+1;    }};隊列/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    //二叉樹最大深度(層次遍曆,遍曆一層高度加1)    int maxDepth(TreeNode *root) {        int height = 0,rowCount = 1;        if(root == NULL){            return 0;        }        //建立隊列        queue<treenode*> queue;        //添加根節點        queue.push(root);        //層次遍曆        while(!queue.empty()){            //隊列頭元素            TreeNode *node = queue.front();            //出隊列            queue.pop();            //一層的元素個數減1,一層遍曆完高度加1            rowCount --;            if(node->left){                queue.push(node->left);            }            if(node->right){                queue.push(node->right);            }            //一層遍曆完            if(rowCount == 0){                //高度加1                height++;                //下一層元素個數                rowCount = queue.size();            }        }        return height;    } };</treenode*> 棧/** * Definition for binary tree * 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 == NULL) return 0;                     stack<treenode*> S;                     int maxDepth = 0;          TreeNode *prev = NULL;                     S.push(root);          while (!S.empty()) {              TreeNode *curr = S.top();                             if (prev == NULL || prev->left == curr || prev->right == curr) {                  if (curr->left)                      S.push(curr->left);                  else if (curr->right)                      S.push(curr->right);              } else if (curr->left == prev) {                  if (curr->right)                      S.push(curr->right);              } else {                  S.pop();              }              prev = curr;              if (S.size() > maxDepth)                  maxDepth = S.size();          }          return maxDepth;      }  };</treenode*> DFS/** * Definition for binary tree * 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 == NULL)return 0;        int res = INT_MIN;        dfs(root, 1, res);        return res;    }    void dfs(TreeNode *root, int depth, int &res)    {        if(root->left == NULL && root->right == NULL && res < depth)            {res = depth; return;}        if(root->left)            dfs(root->left, depth+1, res);        if(root->right)            dfs(root->right, depth+1, res);    }};層次遍曆/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int maxDepth(TreeNode *root) {        //層次遍曆樹的層數,NULL為每一層節點的分割標誌        if(root == NULL)return 0;        int res = 0;        queue<TreeNode*> Q;        Q.push(root);        Q.push(NULL);        while(Q.empty() == false)        {            TreeNode *p = Q.front();            Q.pop();            if(p != NULL)            {                if(p->left)Q.push(p->left);                if(p->right)Q.push(p->right);            }            else             {                res++;                if(Q.empty() == false)Q.push(NULL);            }        }        return res;    }}; 



leetcode是什

裡面有很編程多面試的題目,可以線上編譯運行。難度比較高。如果自己能都做出來,對面大公司很有協助。我就是做的那裡的題目。
 
leetcode網站裡面的題目可以用C語言做?只看到C++,java,與python

C++是相容C語言的啊
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.