Question :
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Anwser 1 : 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL) return 0;
int l = maxDepth(root->left);
int r = maxDepth(root->right);
return l > r ? l + 1 : r + 1;
}
};
More simpe :
class Solution {
public:
int maxDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL) return 0;
return 1 + max( maxDepth(root->left), maxDepth(root->right) );
}
};
Anwser 2 : BFS in queue
class Solution {
public:
int maxDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL) return 0;
queue<TreeNode *> Q;
Q.push(root);
int count = 1;
int depth = 0;
while(!Q.empty()){
TreeNode *tmp = Q.front();
Q.pop();
count--;
if(tmp->left){
Q.push(tmp->left);
}
if(tmp->right){
Q.push(tmp->right);
}
if(count == 0){
depth++; // one row is end, add a depth
count = Q.size(); // next row count
}
}
return depth;
}
};
Anwser 3 : BFS in
stack
class Solution {
public:
int maxDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
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;
}
};
参考推荐:
Maximum Depth of Binary Tree
树与二叉树的深度优先与广度优先算法
分享到:
相关推荐
java java_leetcode-maximum-depth-of-binary-tree
python python_leetcode题解之104_Maximum_Depth_of_Binary_Tree
js js_leetcode题解之104-maximum-depth-of-binary-tree.js
1. Math Implementation Questions(数学实现题) ...5.1 Maximum Depth of Binary Tree(二叉树的深度) 5.2 Invert Binary Tree(反转二叉树) 5.3... 5.4... 5.5... 6. String Questions(字符串相关问题)
Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js103 Binary Tree ...
Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...
leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a ...com.leetcode.list ...com.leetcode.string ...Maximum Depth of Binary Tree Same Tree
java lru leetcode what_the_dead_men_say 所以这只是一个 repo,我从leetcode.com存储我的问题解决方案。 二叉树 0098 Validate Binary ...Tree ...Binary ...Maximum depth of binary tree - Java Iterative
26. Maximum Depth of Binary Tree:计算二叉树的最大深度。 27. Minimum Depth of Binary Tree:计算二叉树的最小深度。 28. Balanced Binary Tree:判断一个二叉树是否是平衡二叉树。 29. Convert Sorted Array to...
LeetCode去除数组重复元素 Arithmetic-Swift 一些算法的swift实现 桶排序 冒泡排序 快速排序 ##正好看见LeetCode可以刷Swift的题目 开始慢慢刷 swift有playground 做起来还是相当方便的 已完成题目 ----2016.9.30 两...
leetcode-js Leecode 经典题目 JavaScript TypeScript 题解。 Leetcode's answers by ...104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)
318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...
- **Depth of Binary Tree**:计算二叉树的深度。 - **Construct Binary Tree**:根据给定的数组构建二叉树。 - **Binary Tree Level Order Traversal**:二叉树的层次遍历。 - **Symmetric Tree**:判断一个...
104.maximum-depth-of-binary-tree (二叉树的最大深度) 105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-...
leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。...Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]
Maximum Depth of Binary Tree 这?也太简单了吧。。一行代码,一个尾递归搞定啊。。 终于想清楚了,leetcode的AC率应该是:在线编辑、肉眼检查,提交的准确率!借助线下debug工具,有何难度可言?丝毫没有模拟在线...
- 计算二叉树的最大深度(Maximum Depth of Binary Tree)考察递归的使用。 - 平衡二叉树(Balanced Binary Tree)问题需要检查一棵树是否是平衡的。 **位操作(Bit Manipulation)** 位操作是高级的编程技能,涉及...
Maximum Depth of Binary Tree"要求计算一棵二叉树的最大深度。 4. 动态规划:动态规划是一种将复杂问题分解为子问题,通过状态转移方程求解的方法。比如,"121. Best Time to Buy and Sell Stock"股票买卖问题,...
26. Maximum Depth of Binary Tree:二叉树的最大深度。 27. Minimum Depth of Binary Tree:二叉树的最小深度。 28. Balanced Binary Tree:判断二叉树是否为平衡二叉树。 29. Convert Sorted Array to Binary ...