classSolution{public:intheightOfTree(TreeNode*root){if(root==nullptr)return0;// a leaf nodeif(root->left!=nullptr&&root->left->right==root)return0;return1+max(heightOfTree(root->left),heightOfTree(root->right));}};
1 2 3 4 5 6 7 8 910
classSolution{publicintheightOfTree(TreeNoderoot){if(root==null)return0;// a leaf nodeif(root.left!=null&&root.left.right==root)return0;return1+Math.max(heightOfTree(root.left),heightOfTree(root.right));}}
12345678
classSolution:defheightOfTree(self,root:TreeNode|None)->int:ifnotroot:return0# a leaf nodeifroot.leftandroot.left.right==root:return0return1+max(self.heightOfTree(root.left),self.heightOfTree(root.right))