C++ Solution, 3 lines, Tree Traversal


  • 0
    B
    class Solution {
    public:
    	TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
    		TreeNode *root=t1 ? (t2 ?
    		new TreeNode(t1->val + t2->val) :new TreeNode(t1->val)) :(t2 ? new TreeNode(t2->val) : NULL);
    		t1 ?(t2 ? (root->left = mergeTrees(t1->left, t2->left), root->right = mergeTrees(t1->right, t2->right)) : (root->left = t1->left, root->right = t1->right)) :(t2 ? (root->left = t2->left, root->right = t2->right ): NULL);
    		return root;
    	}
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.