获取左右子树的高度,如果左右子树高度差小于等于1,则判断左右子树的左右子树,如此递归下去。 class Solution { public: int getDp(TreeNode *root) { if (root == nullptr) return 0; int ldp = getDp(root->left); int rdp = getDp(root
遍历法 遍历所有节点的方法,时间复杂度为\(O(n)\) class Solution { public: int countNodes(TreeNode *root) { if (root == nullptr) return 0; int lc = countNodes(root->left); int rc = countNodes(root->right); return