LeetCode 104. 二叉樹的最大深度 C++三種解法

104. 二叉樹的最大深度

給定一個二叉樹,找出其最大深度。

二叉樹的深度爲根節點到最遠葉子節點的最長路徑上的節點數。

說明: 葉子節點是指沒有子節點的節點。

示例:
給定二叉樹 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

方法一 深度優先遍歷 遞歸

這個方法看一眼就懂了 不講了

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL)
        return 0;
        int depthleft=maxDepth(root->left);
        int depthright=maxDepth(root->right);
        return max(depthleft,depthright)+1;
    }
};

方法二 深度優先遍歷 棧循環

同樣是用到DFS思想,使用棧來保存節點,而且每次進棧的時候要把當前深度保存一下,然後深度遍歷到某一個葉節點,就比較最大深度和當前深度的值,來得到最大的深度。他比較麻煩,需要實時更新當前深度。創建pair同時在棧裏存放結點和當前深度。

核心代碼

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int maxDEp=0;
        stack<pair<TreeNode*,int>>p;
        if(root)
        {
            p.push(pair<TreeNode*,int>(root,1));//放入根節點
        }
        while(!p.empty())
        {
              TreeNode*cur=p.top().first;//棧不爲空取出棧頂元素和當前深度
              int curDep=p.top().second;
              p.pop();//棧頂元素出棧
              maxDEp=maxDEp>curDep? maxDEp:curDep;//和最大深度比較
              if(cur->left) p.push(pair<TreeNode*,int>(cur->left,curDep+1));//這裏最容易想不明白了,我就是被curDep+1忽悠了,雖然這裏加一了,但是它並沒有賦值給curDep,這一點切記,只有當執行到 int curDep=p.top().second時,curDep也就是當前深度纔會加一
              if(cur->right) p.push(pair<TreeNode*,int>(cur->right,curDep+1));
        }
        return maxDEp;//退出循環返回最大值
    }
};

測試代碼

上面的核心程序不懂得,可以直接把我下面的測試代碼複製到VS上Debug調試一下

#include "stdafx.h"
#include<stack>
#include<iostream>
using namespace std;
struct TreeNode
{
	int val;
	struct TreeNode*left;
	struct TreeNode*right;
	struct TreeNode(int data) :val(data), left(nullptr), right(nullptr) {}
};
int maxDepth(TreeNode* root) {
	int maxDEp = 0;
	stack<pair<TreeNode*, int>>p;
	if (root)
	{
		p.push(pair<TreeNode*, int>(root, 1));
	}
	while (!p.empty())
	{
		TreeNode*cur = p.top().first;
		int curDep = p.top().second;
		p.pop();
		maxDEp = maxDEp>curDep ? maxDEp : curDep;
		if (cur->left) p.push(pair<TreeNode*, int>(cur->left, curDep + 1));
		if (cur->right) p.push(pair<TreeNode*, int>(cur->right, curDep + 1));
	}
	return maxDEp;
}
int main()
{
	TreeNode *head = new TreeNode(3);
	head->left = new TreeNode(9);
	head->right = new TreeNode(20);
	/*head->left->left = new TreeNode(2);
	head->left->right = new Node(4);*/
	//head->left->left->left = new Node(1);
	head->right->left = new TreeNode(15);
	//head->right->left->left = new Node(6);
	head->right->right = new TreeNode(7);
	/*head->right->right->left = new Node(9);
	head->right->right->right = new Node(11);*/
	 int a=maxDepth(head);
	 cout << a << endl;
    return 0;
}


方法三 廣度優先搜索 隊列

使用隊列來保存節點,就一層一層遍歷,每次遍歷完一層的節點,深度+1就好了。遍歷到最後一層的深度就是最大深度。這個簡單一些 看代碼容易理解

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth=0;
      deque<TreeNode*>p;
      if(root==NULL)
      return 0;
       p.push_back(root);
      while(!p.empty())
      {
          depth++;
         
          int num=p.size();
          
          for(int i=1;i<=num;i++)
          {   TreeNode*q=p.front();
               p.pop_front();
               if(q->left)p.push_back(q->left);
              if(q->right)p.push_back(q->right);
          }
      }
      return depth;

    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章