顯示具有 tree 標籤的文章。 顯示所有文章
顯示具有 tree 標籤的文章。 顯示所有文章

2022年4月13日 星期三

1038, binary search tree to greater sum tree

1038, binary search tree to greater sum tree
====
tree, BST

====
試想一下一個BST: [4,1,6]
GreaterSum就是 [4+6, 1+4+6, 6], 也就是固定先加總右子 >>> 再來node >>> 再來左子

1. 如何開始: root開始, 照右>中>左

2. 如何結束: 當!node就return, 終究走完

====
class Solution {
public:
  TreeNode* BstToGst(TreeNode* root){
    int sum=0
    SumNode(root, &sum)
    return root
  }
  void SumNode (TreeNode* node, int& sum){
    if(node == NULL) return

    SumNode(node->right, sum)
    sum+=node->val
    node->val = sum
    SumNode(node->left, sum)
  }
}

938, range sum of BST

938, range sum of BST
====
tree, BST
DFS
BFS

====
DFS:
1. 如何開始: 先從root開始
每次都加
node(如果符合range則node->value 否則0), 
node->left, 
node->right

2. 如何結束: 如果本身!node 就return 0

====
class Solution {
public:
  int rangeSumBST(TreeNode* root, int L, int H){
    if(!root) return 0
    return (root->val >= L && root->val <=H ? root->val : 0) + rangeSumBST(root->left, L, H) + rangeSumBST(root->right, L, H)
  }
}

====
BFS:
1. 如何開始: root加到queue, 每次加左子右子

2. 如何停止: queue空

====
class Solution {
public:
  int rangeSumBST(TreeNode* root, int L, int H){
    queue<TreeNode*> pq
    int sum=0
    pq.push(root)
    while(!pq.empty()){
      TreeNode* cur = pq.front
      pq.pop()
      if(cur->val >= L && cur->val <= H)
        sum+=cur->val
      if(cur->left) pq.push(cur->left)
      if(cur->right) pq.push(cur->right)
    }//while pq.empty

  return sum
  }
}

2022年4月11日 星期一

1315, Sum of nodes with even-vlaues grandparent

====
1315, Sum of nodes with even-vlaues grandparent
====
tree,
DFS,
BFS

====
DFS:
1. 照順序 從root檢查 root's parent/grandparent
加總sum(global
左子/右子樹加入dfs

2. 如何起始:root, Null, Null
如何停止:檢查左子右子是否空 才扔DFS

----
class Solution {
public:
  int sum=0
  int sumEvenGradparent(TreeNode* root){
    dfs(root, null, null)
    return sum
  }
  void dfs(TreeNode* node, TreeNode* parent, TreeNode* grandpa){
    if(!root) return
    if(grandpa && grandpa->val%2==0)
      sum += root->val
    if(node->left) dfs(node->left, node, parent)
    if(node->right) dfs(node->right, node, parent)
  }
}

====
BFS:
1. 如何起始:q.push(root)
如何停止:
for整個程式->while q.empty時停止
for加總->如果node為空就return 0, 不然就return node->val

2. q.push左子 右子
應該在while !q.empty內

但是在node->val%2外
因為目的是走過/檢查過所有的node, 因此不侷限在%2的case
----
class Solution{
public:
  int sum=0
  int func(TreeNode* root){
    return root? root->val : 0
  }
  int sumEvenGrandparent(TreeNode* root){
    if(!root) return
    queue<TreeNode*> q
    q.push(root)
    while(!q.empty()){
      TreeNode* node = q.front()
      q.pop()
      if(node->val%2==0){
        if(node->left){
          sum+= func(node->left->left) + func(node->left->right)
        }
        if(node->right){
          sum+= func(node->right->left) + func(node->right->right)
        }
      }//if even

      if(node->left) q.push(node->left)
      if(node->right) q.push(node->right)
    }//while q.empty
    return sum
  }

}

2022年4月10日 星期日

1302, Deepest leaves sum

====
1302, Deepest leaves sum
====
tree
DFS, BFS

====
DFS:
1. 題目給一個TreeNode
從(root, 0//lvl, depth)開始DFS

2. 懶人法, vec<int>sum[i] 紀錄每i層depth的total
在DFS中

如果sum size等於傳入的lvl, 則表示這層depth第一次加入到sum, 所以sum.push_back

否則, 表示已經有index 'lvl', 加入到sum[lvl]
//初始root 0, sum size也零, sum.push_back == sum[0]
//root右子樹 1, sum size卻是2, 表示有左子樹加入過了, 合併到sum[lvl] == sum[1]

3. 最終return vector最後一個元素 == sum.back

----
class Solution {
public:
  vec<int> sum
  int deepestLeaveSum(TreeNode* root){
    dfs(root, 0)
    return sum.back()
  }
  void dfs(TreeNode* node, int lvl){
    if(sum.size() == lvl)
      sum.push_back(node->val)
    else
      sum[lvl] += node->val

    if(node->left) dfs(node->left, lvl+1)
    if(node->right) dfs(node->right, lvl+1)
  }
}

====
BFS:
1. 題目給一個TreeNode
依照lvl/depth加入queue, pop all並加總

2. 懶人法, 當queue不等於空
把sum清空
並且元素pop all並加總//for, 這樣就會得到每一層的sum

如果還有左子右子, push到queue
如果queue空, 表示最後一層leave做完了, return當次/最終sum

----
class Solution {
public:
  int sum
  int deepestLeaveSum(TreeNode* root) {
    queue<TreeNode*> q
    q.push(root)
    while(!q.empty()){
      sum=0
      int qlen = q.size()
      for(int i=0, i<qlen, i++) {
        TreeNode* node = q.front()
        q.pop()
        sum+=node->val

        if(node->left) q.push(node->left)
        if(node->right) q.push(node->right)
      }//for qlen
    }//while q.empty

    return sum
  }
}