Advertisement
golitter

fgb

Jun 14th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     int minDepth(TreeNode* root) {
  15.         if(root == nullptr) {
  16.             return 0;
  17.         } else {
  18.             // return min( minDepth(root->left), minDepth(root->right)) + 1;
  19.             int leftnum = 1e9, rightnum = 1e9;
  20.             if(root->left == nullptr && root->right == nullptr) return 1;
  21.             if(root->left != nullptr) leftnum = minDepth(root->left);
  22.             if(root->right != nullptr) rightnum = minDepth(root->right);
  23.             return min(leftnum, rightnum) + 1;
  24.         }
  25.     }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement