Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees

Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees

669.Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.
You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:
Input: 
    1
   / \
  0   2

  L = 1
  R = 2

Output: 
    1
      \
       2
Example 2:
Input: 
    3
   / \
  0   4
   \
    2
   /
  1

  L = 1
  R = 3

Output: 
      3
     / 
   2   
  /
 1

Solution:

class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int L, int R) {
        if (root == NULL) return NULL;
        if (root->val < L) return trimBST(root->right, L, R);
        if (root->val > R) return trimBST(root->left, L, R);
        root->left = trimBST(root->left, L, R);
        root->right = trimBST(root->right, L, R);
        return root;
    }
};

这道题我一开始考虑递归的时候返回条件考虑的是两个子树都为空的时候返回,写起来的思路就很混乱,多次提交都是RA。后来看了Discussion发现如果思路改为根节点为空时返回的话就可以减少很多不必要的情况讨论。说明对于递归仍然不太熟练,还是得学习一个。

617.Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example 1:
Input: 
    Tree 1                     Tree 2                  
          1                         2                             
         / \                       / \                            
        3   2                     1   3                        
       /                           \   \                      
      5                             4   7                  
Output: 
Merged tree:
         3
        / \
       4   5
      / \   \ 
     5   4   7

做这道题时吸取了上一道题的经验。下面是我的代码——

class Solution {
public:
  TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
    if (t1 == NULL && t2 == NULL) return NULL;
    TreeNode* t3 = new TreeNode(0);
    if (t1 == NULL && t2 != NULL) {
      (*t3).val = t2->val;
      (*t3).left = mergeTrees(NULL, t2->left);
      (*t3).right = mergeTrees(NULL, t2->right);
    } else if (t2 == NULL && t1 != NULL) {
      (*t3).val = t1->val;
      (*t3).left = mergeTrees(t1->left, NULL);
      (*t3).right = mergeTrees(t1->right, NULL);
    } else {
      (*t3).val = t1->val + t2->val;
      (*t3).left = mergeTrees(t1->left, t2->left);
      (*t3).right = mergeTrees(t1->right, t2->right);
    }
    return t3;
  }
};

写完觉得代码冗余部分非常多,后来在Discussion看到这样的答案:

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
        if (!t1 && !t2) {
            return nullptr;
        }
        TreeNode* node = new TreeNode((t1 ? t1->val : 0) + (t2 ? t2->val : 0));
        node->left = mergeTrees((t1 ? t1->left : nullptr), (t2 ? t2->left : nullptr));
        node->right = mergeTrees((t1 ? t1->right : nullptr), (t2 ? t2->right : nullptr));
        return node;
    }
};

写代码时多使用A ? B : C的运算符可以有效减少代码的冗余,减少if else结构的出现,使代码更简洁。

转载于:https://www.cnblogs.com/JerryChan31/p/7538804.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值