-------------------------------------------------two Brush 2021/2/4------------------------------------------
This question Wrong idea when brushing the second
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
return getDepth(root) != -1;
}
int getDepth(TreeNode* root) {
if(root == NULL) return 0;
int left = getDepth(root -> left);
// if(left == -1) return -1;
int right = getDepth(root -> right);
// if(right == -1) return -1;
if(left - right > 1 || right - left > 1) return -1;
return max(left, right) + 1;
}
};
The -1 is not saved and returned to the isBalanced function but as a recursive intermediate value to continue the recursion.
The commented statement should be added. Only then can the child node-1 information be conveyed to the root node layer by layer
55-I
Title description
Idea preorder traversal
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int depth = 1;
int max = 0;
int maxDepth(TreeNode* root) {
dfs(root, 1);
return max;
}
void dfs(TreeNode* root, int d) {
if(root == NULL) return;
max = d > max ? d : max;
dfs(root -> left, d + 1);
dfs(root -> right, d + 1);
}
};
It can also be traversed in post-order
if(root == null) return 0;
return max(maxDepth(root _> left), maxDepth(root -> right)) + 1;
Time complexity O(N)
Space complexity O(N)
55-II
Title description
Idea preorder traversal + judgment depth + pruning
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root == NULL) return true;
if(compare(root) == -1) return false;
return isBalanced(root -> left) && isBalanced(root -> right);
}
int compare(TreeNode* root) {
if(root == NULL) return 0;
int left = compare(root -> left);
int right = compare(root -> right);
if(abs(left - right) > 1) return -1;
return max(left, right) + 1;
}
};
Time complexity O(NlogN)
Space complexity O(N): Degenerate to a linked list The
above code is actually very poor.
First, pruning is not good enough.
When the right subtree is found to be -1, there is no need to judge the left subtree.
Secondly, we actually When traversing and calculating the height of the left and right subtrees, the height difference of the left and right subtrees can be compared in the backtracking process,
so it return isBalanced(root -> left) && isBalanced(root -> right);
is meaningless
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
return compare(root) != -1;
}
int compare(TreeNode* root) {
if(root == NULL) return 0;
int left = compare(root -> left);
if(left == -1) return -1;
int right = compare(root -> right);
if(right == -1) return -1;
if(abs(left - right) > 1) return -1;
return max(left, right) + 1;
}
};
Time complexity O(N)
Space complexity O(N): Degenerate to a linked list