{"id":2551,"date":"2018-05-02T20:13:39","date_gmt":"2018-05-02T12:13:39","guid":{"rendered":"https:\/\/boweihe.me\/?p=2551"},"modified":"2018-05-02T20:13:39","modified_gmt":"2018-05-02T12:13:39","slug":"leetcode-110-balanced-binary-tree","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2551","title":{"rendered":"LeetCode 110. Balanced Binary Tree"},"content":{"rendered":"<p>https:\/\/leetcode.com\/problems\/balanced-binary-tree\/description\/<\/p>\n<pre class=\"lang:c++ decode:true \">\/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    int maxDepth(TreeNode* root, int currDepth) {\n        int left_depth = (root-&gt;left == nullptr) ? currDepth : maxDepth(root-&gt;left, currDepth + 1);\n        int right_depth = (root-&gt;right == nullptr) ? currDepth : maxDepth(root-&gt;right, currDepth + 1);\n        if (left_depth == -1 || right_depth == -1)\n            return -1;\n        if (left_depth - right_depth &gt; 1 ||\n            right_depth - left_depth &gt; 1)\n            return -1;\n        return max(left_depth, right_depth);\n    }\n    bool isBalanced(TreeNode* root) {\n        if (root == nullptr)\n            return true;\n        if (maxDepth(root, 0) == -1)\n            return false;\n        return true;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode.com\/problems\/balanced-binary-tree\/description\/ \/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *\/ class Solution { public: int maxDepth(TreeNode* root, int currDepth) { int left_depth = (root-&gt;left == nullptr) ? currDepth : maxDepth(root-&gt;left, currDepth + [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2551","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2551"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2551\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}