Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree was defined as a binary tree in which the depth of the Every node never differ by more than 1.
Hide TagsTree Depth-first Search A deep search problem.
#include <iostream>#include<stdlib.h>using namespacestd;/** Definition for binary tree*/structTreeNode {intVal; TreeNode*Left ; TreeNode*Right ; TreeNode (intx): Val (x), left (null), right (null) {}};classSolution { Public: BOOLisbalanced (TreeNode *root) { if(Root==null)return true; if(Help_fun (Root) <0)return false; return true; } intHelp_fun (TreeNode *node) { if(Node==null)return 0; intLfT = Help_fun (node->Left ); intRGT = Help_fun (node->Right ); if(lft==-1|| rgt==-1)return-1; if(ABS (LFT-RGT) <2) return(LFT>RGT?LFT:RGT) +1; Else return-1; }};intMain () {return 0;}
[Leetcode] Balanced Binary Tree Depth Search