Given the root of a binary tree, return its maximum depth.
A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Input: root = [3,9,20,null,null,15,7] Output: 3
Input: root = [1,null,2] Output: 2
Input: root = [] Output: 0
Input: root = [0] Output: 1
- The number of nodes in the tree is in the range [0, 104].
- -100 <= Node.val <= 100
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == NULL) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return bfs(root);
}
public int bfs(TreeNode node) {
int level = 0;
Queue<TreeNode> queue = new LinkedList();
queue.offer(node);
while(!queue.isEmpty()) {
int size = queue.size();
level++;
for (int i = 0; i < size; i++) {
TreeNode n = queue.poll();
if (n.left != null) queue.offer(n.left);
if (n.right != null) queue.offer(n.right);
}
}
return level;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
int level = 0;
if (root == nullptr) return level;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
level++;
int size = q.size();
for (int i = 0; i < size; ++i) {
auto node = q.front();
q.pop();
if (node->left != nullptr) q.push(node->left);
if (node->right != nullptr) q.push(node->right);
}
}
return level;
}
};
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return dfs(root);
}
public int dfs(TreeNode node) {
int maxLevel = 0;
Stack<TreeNode> stack = new Stack();
Stack<Integer> depth = new Stack();
stack.push(node);
depth.push(1);
while(!stack.isEmpty()) {
TreeNode n = stack.pop();
int level = depth.pop();
maxLevel = Math.max(maxLevel, level);
if (n.left != null) {
stack.push(n.left);
depth.push(level + 1);
}
if (n.right != null) {
stack.push(n.right);
depth.push(level + 1);
}
}
return maxLevel;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
int maxLevel = 0;
if (root == nullptr)
return maxLevel;
stack<TreeNode*> s;
stack<int> level;
s.push(root);
level.push(1);
while(!s.empty()) {
TreeNode* node = s.top();
s.pop();
int l = level.top();
level.pop();
maxLevel = max(l, maxLevel);
if (node->left != nullptr) {
s.push(node->left);
level.push(l + 1);
}
if (node->right != nullptr) {
s.push(node->right);
level.push(l + 1);
}
}
return maxLevel;
}
};