Balanced Binary Tree
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.
Main topic:
Determine if a binary tree is a balanced binary tree.
Ideas:
Do an auxiliary function to find the height of the tree.
Recursive solution through auxiliary functions.
The code is as follows:
/** * definition for a binary tree node. * struct treenode { * int val; * treenode * Left; * treenode *right; * treenode ( INT&NBSP;X) : val (x), left (null), right (null) {} * }; */class Solution {public: int depth (treenode* root) { if (!root) return 0; int l = Depth (Root->left) ; int r = depth (root- >right) ; return 1 + (l > R) l:r); &nBSP;} bool isbalanced (treenode* root) { if (!root) return true; else { int l = depth (Root->left); int r = depth (root-> right); if (l + 1 < r | | &NBSP;R&NBSP;+&NBSP;1&NBSP;<L) { return false; } & nbsp; else return (isbalanced (root->left) && isbalanced (root-> right) ); } }};
2016-08-08 00:25:26
This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1835477
Leetcode 110. Balanced binary tree Balanced binary trees