二叉树的合并与逆转

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.

Note: The merging process must start from the root nodes of both trees.

Input:

Tree 1 Tree 2
      1                         2                             
     / \                       / \                            
    3   2                     1   3                        
   /                           \   \                      
  5                             4   7                  

Output:

Merged tree:
     3
    / \
   4   5
  / \   \ 
 5   4   7

问题分析:

合并两个二叉树可以当作新生成一个树的问题,所以可以考虑用递归生成树的方法来合并,根据题目,如果两个相同位置的节点都不为null,则将两个节点合并作为新的节点,如果其中一个为空,则直接将另外一个节点作为新的节点,然后递归该节点的左孩子节点与右孩子节点。

算法(C++):

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2){
    if(t1 == NULL)
        return t2;
    if(t2 == NULL)
        return t1;
    TreeNode* t3 = new TreeNode(t1->val + t2->val);
    t3->left = mergeTrees(t1->left, t2->left);
    t3->right = mergeTrees(t1->right, t2->right); 
    return t3;
    }
};

Invert Binary Tree

问题描述:

Invert a binary tree.

tree
     4
   /   \
  2     7
 / \   / \
1   3 6   9

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1

问题分析:

这个问题就是将树的所有节点的左孩子节点跟右孩子节点交换位置,也可以理解为根据所给书生成一个新的树,同样考虑用递归求解,可以新建一个数,根节点与所给树根节点相同,然后将新建树的右孩子赋值为原树的左孩子,左孩子赋值为原树的右孩子,然后递归右孩子的右孩子为原树的左孩子的左孩子,左孩子的左孩子为原书右孩子的右孩子。

算法(C++)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
using namespace std;
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root == NULL) return NULL;
        TreeNode* tmp = new TreeNode(root->val);
        sol(root, tmp);
        return tmp;
    }

    void sol(TreeNode *nowNode, TreeNode *lastNode){
        if(nowNode->left != NULL) {
            lastNode-> right = new TreeNode(nowNode->left->val);
            sol(nowNode->left, lastNode->right);
        }
        if(nowNode->right != NULL) {
            lastNode-> left = new TreeNode(nowNode->right->val);
            sol(nowNode->right, lastNode->left);
        }
    }
};

总结:

二叉树的问题常常要用递归解决,而且要找思路清晰的算法,比如逆转二叉树,我一开始想的是直接递归交换左右孩子节点,但感觉思路很模糊,在原树操作容易出错,还是生成新树思路清晰。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值