中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

網(wǎng)站建設合作范本橙子建站官網(wǎng)

網(wǎng)站建設合作范本,橙子建站官網(wǎng),常州網(wǎng)站推廣招聘,網(wǎng)站分頁怎么做文章目錄二叉樹9. 二叉樹的最大深度10. 二叉樹的最小深度11. 完全二叉樹的節(jié)點個數(shù)12. 平衡二叉樹二叉樹 9. 二叉樹的最大深度 104. 二叉樹的最大深度 思路1: 遞歸找左右子樹的最大深度,選擇最深的 1(即加上當前層)。 class So…

文章目錄

  • 二叉樹
    • 9. 二叉樹的最大深度
    • 10. 二叉樹的最小深度
    • 11. 完全二叉樹的節(jié)點個數(shù)
    • 12. 平衡二叉樹

二叉樹

9. 二叉樹的最大深度

104. 二叉樹的最大深度

思路1:
遞歸找左右子樹的最大深度,選擇最深的 + 1(即加上當前層)。

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

思路2:
利用隊列層序遍歷二叉樹,找到最大深度

class Solution {
public:int maxDepth(TreeNode* root) {int depth = 0;if (root == NULL) return depth;queue<TreeNode *> que;que.push(root);while (!que.empty()) {int size = que.size();depth++;while (size--) {TreeNode *cur = que.front(); que.pop();if (cur->left) que.push(cur->left);if (cur->right) que.push(cur->right);}}return depth;}
};

10. 二叉樹的最小深度

111. 二叉樹的最小深度

思路1: 遞歸法
要注意只有一個子節(jié)點的情況不能統(tǒng)計深度,因為它不是葉子節(jié)點。深度是指根節(jié)點到葉子節(jié)點的距離。

class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL) return 0;// 下面的兩個判斷避免了非葉子節(jié)點的情況if (!root->left && root->right) return minDepth(root->right) + 1;if (root->left && !root->right) return minDepth(root->left) + 1;return min(minDepth(root->left), minDepth(root->right)) + 1;}
};

思路2:
利用迭代法層序遍歷,遇到第一個葉子節(jié)點返回深度。

class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL) return 0;queue<TreeNode *> que;int depth = 0;que.push(root);while (!que.empty()) {depth++;int size = que.size();while (size--) {TreeNode *cur = que.front(); que.pop();if (!cur->left && !cur->right) return depth;if (cur->left) que.push(cur->left);if (cur->right) que.push(cur->right);}}return depth;}
};

11. 完全二叉樹的節(jié)點個數(shù)

222. 完全二叉樹的節(jié)點個數(shù)

思路:
滿二叉樹的節(jié)點數(shù)目等于 2depth?12^{depth} - 12depth?1 ,利用該條件來避免遍歷整個滿二叉樹,只需要遍歷其最左最右兩分支的深度即可(O(logn)O(logn)O(logn))。遍歷二叉樹的遞歸層數(shù) O(logn)O(logn)O(logn) 。時間復雜度是 O(logn?logn)O(logn*logn)O(logn?logn)

class Solution {
public:int countNodes(TreeNode* root) {if (root == NULL) return 0;// 利用滿二叉樹的特性優(yōu)化時間復雜度TreeNode *curL = root->left;TreeNode *curR = root->right;int depthL = 0;int depthR = 0;while (curL != NULL) {curL = curL->left;depthL++;}while (curR != NULL) {curR = curR->right;depthR++;}if (depthL == depthR) return (2 << depthL) - 1;return countNodes(root->left) + countNodes(root->right) + 1;}
};

12. 平衡二叉樹

110. 平衡二叉樹

思路1: 遞歸
后序遍歷統(tǒng)計左右子樹的高度,比較左右子樹高度是否滿足條件。

class Solution {
public:bool isBalanced(TreeNode* root) {if (root == NULL) return true;return getHigh(root) == -1 ? false : true;}private:int getHigh(TreeNode *root) {if (root == NULL) return 0;int leftDepth = getHigh(root->left);if (leftDepth == -1) return -1;int rightDepth = getHigh(root->right);if (rightDepth == -1) return -1;if (abs(leftDepth - rightDepth) > 1) return -1; // 如果已經(jīng)找到不滿足底子樹,就沒必要再遍歷了。剪枝return max(leftDepth, rightDepth) + 1;}
};

思路2: 迭代法
用棧模擬遞歸實現(xiàn)后續(xù)遍歷統(tǒng)計二叉樹深度。
再先序遍歷判斷左右子樹深度是否滿足條件。
這里沒法剪枝,復雜度并不優(yōu)秀。

class Solution {
public:bool isBalanced(TreeNode* root) {if (root == NULL) return true;stack<TreeNode *> stk;stk.push(root);while (!stk.empty()) {TreeNode *cur = stk.top(); stk.pop();if (abs(getHigh(cur->left) - getHigh(cur->right)) > 1) return false;if (cur->right) stk.push(cur->right);if (cur->left) stk.push(cur->left);}return true;}private:int getHigh(TreeNode *root) {if (root == NULL) return 0;stack<TreeNode *> stk;int depth = 0, result = 0;stk.push(root);while (!stk.empty()) {TreeNode *cur = stk.top(); stk.pop();if (cur) { // 后序遍歷:左右中stk.push(cur); // 中stk.push(NULL);depth++;if (cur->right) stk.push(cur->right); // 右if (cur->left) stk.push(cur->left); // 左} else {cur = stk.top(); stk.pop();depth--; // 回溯}result = result > depth ? result : depth;}return result;}
};
http://www.risenshineclean.com/news/11978.html

相關(guān)文章:

  • 手游代理平臺哪個好seo專員是什么
  • 做外貿(mào)批發(fā)的網(wǎng)站有哪些seo搜索引擎優(yōu)化實訓
  • dedecms農(nóng)業(yè)種植網(wǎng)站模板蘇州seo關(guān)鍵詞優(yōu)化方法
  • 廈門網(wǎng)站建設公司新網(wǎng)域名
  • 中國能建官網(wǎng)seo短視頻網(wǎng)頁入口引流免費
  • 馬來西亞做網(wǎng)站一鍵搭建網(wǎng)站
  • wordpress如何安裝網(wǎng)站主題seo平臺怎么樣
  • 東營網(wǎng)站建設公司臨沂頭條新聞今日頭條
  • 小型b2c網(wǎng)站營銷推廣活動策劃書模板
  • 青島建網(wǎng)站人如何查詢域名注冊人信息
  • 大企業(yè)網(wǎng)站建設方案seo關(guān)鍵詞排名優(yōu)化軟件
  • 香港做批發(fā)的網(wǎng)站有哪些手續(xù)灰色行業(yè)怎么推廣引流
  • 陽泉住房和城鄉(xiāng)建設部網(wǎng)站百度官方免費下載
  • 做網(wǎng)站原型圖用什么軟件網(wǎng)絡服務投訴平臺
  • 網(wǎng)站建設好了怎么進行推廣會計培訓班哪個機構(gòu)比較好
  • 長治網(wǎng)站建設哪家好自助建站平臺源碼
  • 公司備案網(wǎng)站被注銷嗎杭州網(wǎng)站建設技術(shù)支持
  • 黑群暉做網(wǎng)站seo簡介
  • 做民宿要給網(wǎng)站多少合同錢專業(yè)的網(wǎng)站優(yōu)化公司排名
  • 淘寶聯(lián)盟做網(wǎng)站友情鏈接平臺哪個好
  • 肇慶企業(yè)做網(wǎng)站品牌推廣方案模板
  • 招聘網(wǎng)頁設計師全域seo
  • 去施工網(wǎng)seo獨立站
  • 哪個網(wǎng)站的pc端是用vue做的整站優(yōu)化外包服務
  • 不買域名怎么做網(wǎng)站不要手賤搜這15個關(guān)鍵詞
  • 怎么做高端品牌網(wǎng)站設計科技公司網(wǎng)站制作公司
  • 網(wǎng)站網(wǎng)站制作服務自助建站系統(tǒng)源碼
  • 快捷做網(wǎng)站網(wǎng)絡營銷的基本功能
  • 網(wǎng)站建設多少錢一個站世界十大網(wǎng)站排名出爐
  • flash相冊網(wǎng)站源碼旅行網(wǎng)站排名前十名