Skip to content

Latest commit

 

History

History
57 lines (45 loc) · 1.09 KB

depth_of_binarytree.md

File metadata and controls

57 lines (45 loc) · 1.09 KB

104. Maximum Depth of Binary Tree

can do via

  • level order traversal(iteratively)
  • via recursive(DFS type).
iterative
class Solution {
  public:
  int maxDepth(TreeNode* root) {
    int ans = 0;
    queue<TreeNode*> qu;
    if (root == nullptr) return 0;

    qu.push(root);

    while (!qu.empty()) {
      int Size = qu.size();
      ans ++;
      for (int i = 0; i < Size; i++) {
        auto top = qu.front();
        qu.pop();


        if (top -> left) qu.push(top -> left);
        if (top -> right) qu.push(top -> right);
      }
    }
    return ans;

  }
};
recursive
class Solution {
  public:
  int maxDepth(TreeNode* root) {
    auto fun = [&](const auto& self, const auto& root) -> int {
      return root == nullptr ? 0
                             : max(self(self, root -> right),
                                   self(self, root -> left)) + 1;
    };
    return fun(fun, root);
  }
};