Recursive - leetcode [递归]

时间:2023-03-09 13:43:04
Recursive - leetcode [递归]

经验tips:

Recursion is the best friend of tree-related problems.

一是只要遇到字符串的子序列或配准问题首先考虑动态规划DP,二是只要遇到需要求出所有可能情况首先考虑用递归。

93 - restore IP address

注:

A。在return条件之前先判断valid的条件 1, max bits per partition[size() - startIndex <= (4 - parts) * 3] 2, min bit per partiton[1].否则在1111 的情况会重复输出level直接被push进result里。

B。recursive语句在for循环的if的判断语句里。

95 - unique binary search tree II

对于第i个node generate from 1~i - 1 给左子树 i+1~n右子树。

三层循环: 第i个node vector<TreeNode*> left left[j] right[k] 最里层循环new节点i 赋值i, i->left, i->right。然后push进vector tree里。最后return tree。

循环前加判断条件:当start > end 直接push NULL 然后return tree。

98. Validate Binary Search Tree

空树为真。

中序遍历,用lastnode记录并比较 如果大于当前node的val就return false。

判断条件:

A。if(!isValid(root ->left)) return false

B。if(!lastnode && lastnode->val>=root->val) return false  =====>   lastnode往下走 = root

C。return isValid(root -> right)

100. Same Tree

对每一个点的判断条件有三个:

A。同时为空 真

B。不同时为空 假

C。值不同 假

return isSame(p->left,q->left)&&isSame(p->right,q->right);

101. Symmetric Tree

判断方法与same tree 基本相同 最后recursive的时候左右树的左右子树对称比较。

104. Maximum Depth of Binary Tree

最底层:root == NULL时 return 0;

每一层会加一:return (l > r) ? l + 1 : r + 1;

int l = maxDepth(root -> left);

105. Construct Binary Tree from Preorder and Inorder Traversal

preorder第一个是root。在inorder中找root,然后分别generate左右子树。

typedef vector<int>::iterator Iter;

开始要判断是否为空树。

106. Construct Binary Tree from Inorder and Postorder Traversal

postorder最后一个是root。

108. Convert Sorted Array to Binary Search Tree

找median。先new root,左右分别recursive调用generate左右子树。

109. Convert Sorted List to Binary Search Tree

中序遍历。先generate左子树用变量存起来,再连接到root上,再root->right = generate(list,mid,end);

要先过一遍list求size。

110. Balanced Binary Tree

Maximum depth of binary tree的变形。

A。空树 return 0

B。不符合判断 return -1

C。依然return (l > r) ? l + 1:r+1。返回depth

主程序判断:只要返回值为-1就return false

111. Minimum Depth of Binary Tree

最底层:return条件:为空,返回0;为叶子节点,返回1。

与max depth不同的地方是return条件 +1换地方了。

最后return min(l,r)的时候别忘了加根。+ 1

112. Path Sum

return的是布尔值。

bool helper(TreeNode* root, int sum, int val){
  if(root == NULL) return false;
  val += root -> val;//语句顺序!!!!!
  if(val == sum && !root -> left && !root -> right){
    return true;
  }
  //val += root -> val;
  return helper(root -> left, sum, val)||helper(root -> right, sum, val);
}

113. Path Sum II

return为空。

每一层level pop的时候是在dfs完左右子树的时候。

result.push_back(level)的条件成立时 并不返回。原因是如果不在遍历完左右子树的时候返回,level里面的值会多出。

void dfs(TreeNode* root, int sum, vector<vector<int>>& result, vector<int>& level, int val){
  if(root == NULL) return;
  level.push_back(root -> val);//注意:对称结构在最外层!!!!!
  val += root -> val;
  if(val == sum && !root -> left && !root -> right){
    result.push_back(level);
    //return;
  }
  //while(root){
  else{
    if(root -> left){
      //level.push_back(root -> left -> val);
      dfs(root -> left, sum, result, level, val);
      //level.pop_back();
    }
    if(root -> right){
      //level.push_back(root -> right -> val);
      dfs(root -> right, sum, result, level, val);
      //level.pop_back();
    }
  }
  level.pop_back();
  val -= root -> val;
  return;////为啥在此处return啊!!!????

}

12/15/16

124. Binary Tree Maximum Path Sum

注:传的是节点的值 不是高度 最后return max(left, right) + root -> val;

但主程序返回的并不是recursion的值 而是全局变量ans

12/17/16

129. Sum Root to Leaf Numbers

Recursion. Similar as [LeetCode] Binary Tree Maximum Path Sum Solution, the difference here is only adding a track variable to sum all the paths.

void generate(TreeNode* root, int sum, int path){
  if(root == NULL) return;
  path = path * 10 + root -> val;
  if(root -> left == NULL && root -> right == NULL){
    sum += path;
    return;
  }
  generate(root -> left, sum, path);
  generate(root -> right, sum, path);
}

12/19/16

133. Clone Graph

注意:别忘了初始化table!!!!
table[newnode -> label] = newnode;