1. 程式人生 > >LeetCode 617 Merge Two Binary Trees 合併二叉樹

LeetCode 617 Merge Two Binary Trees 合併二叉樹

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

相關推薦

LeetCode 617 Merge Two Binary Trees 合併

class Solution { public:     TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {         if(t1 && t2)         {             TreeN

[LeetCode] 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.

Leetcode617.Merge Two Binary Trees合併

給定兩個二叉樹,想象當你將它們中的一個覆蓋到另一個上時,兩個二叉樹的一些節點便會重疊。 你需要將他們合併為一個新的二叉樹。合併的規則是如果兩個節點重疊,那麼將他們的值相加作為節點合併後的新值,否則不為 NULL 的節點將直接作為新二叉樹的節點。   stru

LeetCode 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 oth

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 whi

17. Merge Two Binary Trees 融合

col number bug use .com info ber cor tree [抄題]: Given two binary trees and imagine that when you put one of them to cover the other, some

[leetcode-617-Merge Two Binary Trees]

sed nbsp clas style color tput inpu right into Given two binary trees and imagine that when you put one of them to cover the other, some

[LeetCode] 617. Merge Two Binary Trees

left treenode 生成 must 我沒 += lee 二叉樹 lap Given two binary trees and imagine that when you put one of them to cover the other, some nodes

leetcode 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

JavaScript刷LeetCode -- 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 th

LeetCode(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 ot

617. Merge Two Binary Trees

child treenode ron while val nodes into cal problem https://leetcode.com/problems/merge-two-binary-trees/#/description Given two binary

617. Merge Two Binary Trees - Easy

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.

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

617. Merge Two Binary Trees(Tree)

https://leetcode.com/problems/merge-two-binary-trees/description/ 題目:合併2棵二叉樹 思路:採用後序遍歷的方法,分為3種情況: 1 如果t1非空並且t2非空,則t1加上t2的值,返回t1,否則直接返回t1 2

LeetCode 543. Diameter of Binary Tree (的直徑)

tween res edge public level 距離 sent java dot Given a binary tree, you need to compute the length of the diameter of the tree. The diamet

Aizu - ALDS1_7_B Binary Trees的表達)

二叉 orm print sts ica nod nts sin ostream A rooted binary tree is a tree with a root node in which every node has at most two children.

LeetCode Construct String from Binary Tree 根據建立字串

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be

[LeetCode] Construct String from Binary Tree 根據建立字串

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be repre

[LeetCode] Find Leaves of Binary Tree 找的葉節點

Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example: Given binary tree