leetcode linked list
tree
intro
树是一种递归结构,很多树的问题可以使用递归来处理。
10-06
1. max depth
Depth-first-search
int maxDepth(TreeNode *root)
{
return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
}
Breadth-first-search
Calculate the count of the last level.
int maxDepth(TreeNode *root)
{
if(root == NULL)
return 0;
int res = 0;
queue<TreeNode *> q;
q.push(root);
while(!q.empty())
{
++ res;
for(int i = 0, n = q.size(); i < n; ++ i)
{
TreeNode *p = q.front();
q.pop();
if(p -> left != NULL)
q.push(p -> left);
if(p -> right != NULL)
q.push(p -> right);
}
}
return res;
}
2. Balanced tree(?)
int depth (TreeNode *root) {
if (root == NULL) return 0;
return max (depth(root -> left), depth (root -> right)) + 1;
}
bool isBalanced (TreeNode *root) {
if (root == NULL) return true;
int left=depth(root->left);
int right=depth(root->right);
return abs(left - right) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
[?]In this bottom up approach, each node in the tree only need to be accessed once. Thus the time complexity is O(N), better than the first solution.
class solution {
public:
int dfsHeight (TreeNode *root) {
if (root == NULL) return 0;
int leftHeight = dfsHeight (root -> left);
if (leftHeight == -1) return -1;
int rightHeight = dfsHeight (root -> right);
if (rightHeight == -1) return -1;
if (abs(leftHeight - rightHeight) > 1) return -1;
return max (leftHeight, rightHeight) + 1;
}
bool isBalanced(TreeNode *root) {
return dfsHeight (root) != -1;
}
};
3.Diameter of Binary Tree(?)
class Solution {
private: int maxd = 0;
public: int diameterOfBinaryTree(TreeNode *root) {
depth(root);
return maxd;
}
private:
int depth(TreeNode *root) {
if (!root) return 0;
int leftDepth = depth(root->left);
int rightDepth = depth(root->right);
maxd = max(maxd, leftDepth + rightDepth);
return max(leftDepth, rightDepth) + 1;
}
};
4 invert a binary tree(?)
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL){return NULL;}
TreeNode* invertedleft = invertTree(root->left);
TreeNode* invertedright = invertTree(root->right);
root->left = invertedright;
root->right = invertedleft;
return root;
}
};
add two binary tree
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if(t1 == NULL){
return t2;
}
if(t2 == NULL){
return t1;
}
t1->val += t2->val;
t1->left = mergeTrees(t1->left,t2->left);
t1->right = mergeTrees(t1->right,t2->right);
return t1;
}
};
10-07
search sum
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root == NULL) return false;
if(root->val==sum&&root->left==NULL&&root->right==NULL) return true;
return (hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val));
}
};
437. Path Sum III(?)
OOP solution
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
if(root){
dfs(root,sum);
pathSum(root->left,sum);
pathSum(root->right,sum);
}
return ans;
}
private:
int ans=0;
void dfs(TreeNode* root, int sum){
if(!root)return;
if(root->val==sum)ans++;
dfs(root->left,sum-root->val);
dfs(root->right,sum-root->val);
}
};
subtree(?)
exists better solution
class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
if (s == NULL) return false;
return rootSubtree(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t);
}
private:
bool rootSubtree(TreeNode* s, TreeNode* t){
if(!s && !t) return true;
if(!s || !t) return false;
if(t->val != s->val) return false;
return rootSubtree(s->left,t->left) && rootSubtree(s->right,t->right);
}
};
[?] BETTER SOL
class Solution {
vector<TreeNode*> nodes;
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
// if (!s && !t) return true;
// if (!s || !t) return false;
getDepth(s, getDepth(t, -1));
for (TreeNode* n: nodes)
if (identical(n, t))
return true;
return false;
}
int getDepth(TreeNode* r, int d) {
if (r==NULL)
return -1;
int depth = max(getDepth(r->left, d), getDepth(r->right, d)) + 1;
// Check if depth equals required value
// Require depth is -1 for tree t (only return the depth, no push)
if (depth == d)
nodes.push_back(r);
return depth;
}
bool identical(TreeNode* a, TreeNode* b) {
if (!a && !b) return true;
if (!a || !b || a->val != b->val) return false;
return identical(a->left, b->left) && identical(a->right, b->right);
}
};
symmetric tree(?)
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
return mirror(root->left, root->right);
}
bool mirror(TreeNode* l, TreeNode* r){
if(l == NULL && r == NULL) return true;
if(l == NULL || r == NULL) return false;
if(l->val != r ->val) return false;
return mirror(l->left,r->right) && mirror(l->right,r->left);
}
};
[?] iterative in DFS
bool isSymmetric(TreeNode* root) {
if(!root) return true;
stack<TreeNode*> sl, sr;
sl.push(root);
sr.push(root);
TreeNode * lp = root->left, *rp = root->right;
while(lp || ! sl.empty() || rp || !sl.empty()){
if((!lp && rp) || (lp && !rp)) return false;
if(lp && rp){
if(lp->val != rp->val) return false;
sl.push(lp);
sr.push(rp);
lp = lp->left;
rp = rp->right;
}else{
lp = sl.top()->right;
rp = sr.top()->left;
sl.pop();
sr.pop();
}
}
return true;
}
mindepth(😃)
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return 0;
if(root->left == NULL){return minDepth(root->right)+1;}
if(root->right == NULL){return minDepth(root->left)+1;}
return min(minDepth(root->left),minDepth(root->right))+1;
}
};
10-08
Left leaves(?)
commented out solution is wrong but why?
class Solution {
public:
// int sumOfLeftLeaves(TreeNode* root) {
// if(root == NULL || root->left == NULL) return 0;
// if(root->left!=NULL && root->left->left==NULL && root->left->right==NULL){
// return root->left->val + sumOfLeftLeaves(root->right);
// }
// return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
// }
public:
int sumOfLeftLeaves(TreeNode* root) {
if (root == NULL) return 0;
if (isLeaf(root->left)) return root->left->val + sumOfLeftLeaves(root->right);
return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
private:
bool isLeaf(TreeNode* node){
if (node == NULL) return false;
return node->left == NULL && node->right == NULL;
}
};