LCOV - code coverage report
Current view: top level - src/src - 331.verify-preorder-serialization-of-a-binary-tree.cpp (source / functions) Coverage Total Hit
Test: _coverage_report.dat Lines: 92.3 % 26 24
Test Date: 2025-07-24 17:41:04 Functions: 100.0 % 5 5

            Line data    Source code
       1              : // Copyright (c) 2024 The Authors. All rights reserved.
       2              : //
       3              : // Licensed under the Apache License, Version 2.0 (the "License");
       4              : // you may not use this file except in compliance with the License.
       5              : // You may obtain a copy of the License at
       6              : //
       7              : //      https://www.apache.org/licenses/LICENSE-2.0
       8              : //
       9              : // Unless required by applicable law or agreed to in writing, software
      10              : // distributed under the License is distributed on an "AS IS" BASIS,
      11              : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12              : // See the License for the specific language governing permissions and
      13              : // limitations under the License.
      14              : 
      15              : // Authors: liubang (it.liubang@gmail.com)
      16              : 
      17              : #include <gtest/gtest.h>
      18              : 
      19              : #include <string>
      20              : 
      21              : namespace {
      22              : class Solution {
      23              : public:
      24            4 :     bool isValidSerialization(const std::string& preorder) {
      25            4 :         if (preorder == "#")
      26            0 :             return false;
      27            4 :         int len = preorder.length(), inDegree = 0, outDegree = 0;
      28           45 :         for (int i = 0; i < len; ++i) {
      29           42 :             if (preorder[i] == ',')
      30           19 :                 continue;
      31           23 :             if (i == 0) {
      32            4 :                 if (preorder[i] == '#')
      33            0 :                     return false;
      34            4 :                 outDegree += 2;
      35              :             } else {
      36           19 :                 if (preorder[i] != '#') {
      37            6 :                     outDegree += 2;
      38              :                 }
      39           19 :                 inDegree++;
      40              :             }
      41           23 :             if (i != len - 1 && inDegree >= outDegree) {
      42            1 :                 return false;
      43              :             }
      44           26 :             while ((i + 1 < len && preorder[i + 1] != ',') || (i + 1 == len)) {
      45            4 :                 i++;
      46              :             }
      47              :         }
      48            3 :         return inDegree == outDegree;
      49              :     }
      50              : };
      51              : } // namespace
      52              : 
      53            4 : TEST(Leetcode, verify_preorder_serialization_of_a_binary_tree) {
      54            1 :     Solution s;
      55              : 
      56            1 :     EXPECT_TRUE(s.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"));
      57            1 :     EXPECT_TRUE(s.isValidSerialization("9,#,92,#,#"));
      58            1 :     EXPECT_FALSE(s.isValidSerialization("1,#"));
      59            1 :     EXPECT_FALSE(s.isValidSerialization("9,#,#,1"));
      60            1 : }
        

Generated by: LCOV version 2.0-1