1. 程式人生 > 實用技巧 >《Hadoop實戰》之聯結不同來源的資料

《Hadoop實戰》之聯結不同來源的資料

Leetcode 617 合併二叉樹

資料結構定義:

給定兩個二叉樹,想象當你將它們中的一個覆蓋到另一個上時,兩個二叉樹的一些節點便會重疊。

你需要將他們合併為一個新的二叉樹。合併的規則是如果兩個節點重疊,那麼將他們的值相加作為節點合併後的新值,否則不為NULL 的節點將直接作為新二叉樹的節點。

示例1:

輸入: 
	Tree 1                     Tree 2                  
          1                         2                             
         / \                       / \                            
        3   2                     1   3                        
       /                           \   \                      
      5                             4   7                  
輸出: 
合併後的樹:
	     3
	    / \
	   4   5
	  / \   \ 
	 5   4   7
注意:合併必須從兩個樹的根節點開始。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

遞迴方式:

class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if(t1 == null && t2 == null)
            return null;
        TreeNode node = new TreeNode();
        if(t1 != null)
            node.val = t1.val;
        if(t2 != null)
            node.val += t2.val;
        node.left = mergeTrees(t1 == null ? null: t1.left,
                               t2 == null ? null: t2.left);
        node.right = mergeTrees(t1 == null ? null: t1.right,
                                t2  == null ? null: t2.right);
        return node;
    }
}

另一種遞迴改進方式:

/*
* 不是都建立一個新的節點
*/
class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if(t1 == null)
            return t2;
        if(t2 == null)
            return t1;
        TreeNode node = new TreeNode(t1.val + t2.val);
        node.left = mergeTrees(t1.left,t2.left);
        node.right = mergeTrees(t1.right,t2.right);
        return node;
    }
}

廣度優先遍歷方式:

/*
* 思路: 定義三個佇列,分別儲存合併後的二叉樹的節點以及兩個原始二叉樹的節點
*		定義一個節點,記錄左右子樹的連線情況
*/
class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if(t1 == null)
            return t2;
        if(t2 == null)
            return t1;
        Queue<TreeNode> queue =new LinkedList<>();
        Queue<TreeNode> queueT1 =new LinkedList<>();
        Queue<TreeNode> queueT2 = new LinkedList<>();
        TreeNode node = new TreeNode(t1.val + t2.val);
        queue.offer(node);
        queueT1.offer(t1);
        queueT2.offer(t2);
        while(!queueT2.isEmpty() && ! queueT1.isEmpty()){
            TreeNode temp = queue.poll();
            TreeNode node1 = queueT1.poll();
            TreeNode node2 = queueT2.poll();
            TreeNode left1 = node1.left,left2 = node2.left;
            TreeNode right1 = node1.right,right2 = node2.right;
            temp.left = getSubTreeNode(left1,left2,queue,queueT1,queueT2);
            temp.right= getSubTreeNode(right1,right2,queue,queueT1,queueT2);
        }
        return node;
    }
    private TreeNode getSubTreeNode(TreeNode t1,TreeNode t2,
                         Queue<TreeNode> queue,
                         Queue<TreeNode> queueT1,
                         Queue<TreeNode> queueT2){
        if(t1 != null && t2 != null){
            TreeNode node = new TreeNode(t1.val + t2.val);
            queueT1.offer(t1);
            queueT2.offer(t2);
            queue.offer(node);
            return node;
        }else{
            return t1 != null ? t1 : t2;
        }
    }
}