https://oj.leetcode.com/problems/balanced-binary-tree/
http://blog.csdn.net/linhuanmars/article/details/23731355
/** * definition for binary tree * public class treenode { * int val; * TreeNode left; * treenode right; * treenode (int &NBSP;X) { val = x; } * } */public class solution { public boolean isbalanced (treenode root) { return visit (Root). balanced; } ///////////////////////// // Solution a: dfs // // by definition: depth of two subtrees never differ by more than 1. private result&Nbsp;visit (Treenode node) { if (Node == null) return new result (0, true); result left = visit (Node.left); if (!left.balanced) return new result (0, false); Result right = Visit (node.right); if (!right.balanced) return new result (0, false); int height = math.max (left.height, Right.height) + 1; boolean balanced = Math.Abs (left.height - right.height) <= 1; return new result (height, balanced); } private static class Result { int height; boolean Balanced; result (int height, boolean balanced) { this.height = height; this.balanced = balanced; } }}
[leetcode]110 Balanced Binary Tree