Use exception handling to early terminate and return once a unbalance is found
Has anyone tried using Java exception handling to early terminate recursion function once a unbalance node is found?\n\n    public class Solution {\n        pub
Has anyone tried using Java exception handling to early terminate recursion function once a unbalance node is found?\n\n    public class Solution {\n        public boolean isBalanced(TreeNode root) {\n            try {\n                getDepth(root);\n            } catch (Exception e) {\n                return false;\n            }\n            return true;\n        }\n        private int getDepth(TreeNode root) throws Exception {\n            if (root == null) {\n                return 0;\n            }\n            int leftDepth = getDepth(root.left);\n            int rightDepth = getDepth(root.right);\n            if (Math.abs(leftDepth - rightDepth) > 1) {\n                throw new Exception("Not balanced!");\n            }\n            return Math.max(leftDepth, rightDepth) + 1;\n        }\n    }
['Binary Tree', 'Java']