• Week2


    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结构的出现,使代码更简洁。

  • 相关阅读:
    JAVA DBUTils和连接池
    JAVA jsp技术
    java cookie学习
    grub引导
    更改网卡顺序及名称
    ambari 警告信息
    radhat7.2 救援模式
    radhat7.2 系统引导修复
    sehll编程入门
    HBase安装部署
  • 原文地址:https://www.cnblogs.com/JerryChan31/p/7538804.html
Copyright © 2020-2023  润新知