Leet Code :: Merge Binary Tree :: Time Complexity [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-3
down vote
favorite
Problem Statement: 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.
My solution for the problem:
/**
* 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)
// in case if either is null, directly return one of the nodes depending on the availablity.
if(t1==null) return t2;
if(t2==null) return t1;
TreeNode left=mergeTrees(t1.left,t2.left);
TreeNode right=mergeTrees(t1.right,t2.right);
t1.val=t1.val+t2.val;
t1.left=left;
t1.right=right;
t2=null;
return t1;
I have calculated the worst-case time-complexity
of the above code as O(Min.(Size of t1,Size of t2))
but I am not sure about it? Please share your analysis of it.
java algorithm time-limit-exceeded complexity
closed as off-topic by Antot, Mast, Sam Onela, t3chb0t, Ludisposed Jan 8 at 8:47
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions must involve real code that you own or maintain. Pseudocode, hypothetical code, or stub code should be replaced by a concrete implementation. Questions seeking an explanation of someone else's code are also off-topic." â Antot, Mast, Sam Onela, t3chb0t, Ludisposed
 |Â
show 1 more comment
up vote
-3
down vote
favorite
Problem Statement: 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.
My solution for the problem:
/**
* 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)
// in case if either is null, directly return one of the nodes depending on the availablity.
if(t1==null) return t2;
if(t2==null) return t1;
TreeNode left=mergeTrees(t1.left,t2.left);
TreeNode right=mergeTrees(t1.right,t2.right);
t1.val=t1.val+t2.val;
t1.left=left;
t1.right=right;
t2=null;
return t1;
I have calculated the worst-case time-complexity
of the above code as O(Min.(Size of t1,Size of t2))
but I am not sure about it? Please share your analysis of it.
java algorithm time-limit-exceeded complexity
closed as off-topic by Antot, Mast, Sam Onela, t3chb0t, Ludisposed Jan 8 at 8:47
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions must involve real code that you own or maintain. Pseudocode, hypothetical code, or stub code should be replaced by a concrete implementation. Questions seeking an explanation of someone else's code are also off-topic." â Antot, Mast, Sam Onela, t3chb0t, Ludisposed
@Antot If you think it is a duplicate question, mark it as a duplicate.
â pacmaninbw
Jan 6 at 15:14
@pacmaninbw: it's not duplicate, it's about code that copy-pasted from elsewhere.
â Antot
Jan 6 at 15:24
I suggest you ask a question on codereview.meta.stackexchange.com/questions about this. Unless it is copying your code I don't think there is a rule here about copy and paste.
â pacmaninbw
Jan 6 at 15:32
@pacmaninbw I think the help center states it has to be code you own.
â Mast
Jan 6 at 15:52
@Antot it is my solution, the solution you have shared is different. it creates a new tree entirely where as this solution uses one of the trees in provided in the input.
â Abhiroj Panwar
Jan 6 at 16:42
 |Â
show 1 more comment
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
Problem Statement: 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.
My solution for the problem:
/**
* 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)
// in case if either is null, directly return one of the nodes depending on the availablity.
if(t1==null) return t2;
if(t2==null) return t1;
TreeNode left=mergeTrees(t1.left,t2.left);
TreeNode right=mergeTrees(t1.right,t2.right);
t1.val=t1.val+t2.val;
t1.left=left;
t1.right=right;
t2=null;
return t1;
I have calculated the worst-case time-complexity
of the above code as O(Min.(Size of t1,Size of t2))
but I am not sure about it? Please share your analysis of it.
java algorithm time-limit-exceeded complexity
Problem Statement: 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.
My solution for the problem:
/**
* 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)
// in case if either is null, directly return one of the nodes depending on the availablity.
if(t1==null) return t2;
if(t2==null) return t1;
TreeNode left=mergeTrees(t1.left,t2.left);
TreeNode right=mergeTrees(t1.right,t2.right);
t1.val=t1.val+t2.val;
t1.left=left;
t1.right=right;
t2=null;
return t1;
I have calculated the worst-case time-complexity
of the above code as O(Min.(Size of t1,Size of t2))
but I am not sure about it? Please share your analysis of it.
java algorithm time-limit-exceeded complexity
asked Jan 6 at 11:10
Abhiroj Panwar
973
973
closed as off-topic by Antot, Mast, Sam Onela, t3chb0t, Ludisposed Jan 8 at 8:47
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions must involve real code that you own or maintain. Pseudocode, hypothetical code, or stub code should be replaced by a concrete implementation. Questions seeking an explanation of someone else's code are also off-topic." â Antot, Mast, Sam Onela, t3chb0t, Ludisposed
closed as off-topic by Antot, Mast, Sam Onela, t3chb0t, Ludisposed Jan 8 at 8:47
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions must involve real code that you own or maintain. Pseudocode, hypothetical code, or stub code should be replaced by a concrete implementation. Questions seeking an explanation of someone else's code are also off-topic." â Antot, Mast, Sam Onela, t3chb0t, Ludisposed
@Antot If you think it is a duplicate question, mark it as a duplicate.
â pacmaninbw
Jan 6 at 15:14
@pacmaninbw: it's not duplicate, it's about code that copy-pasted from elsewhere.
â Antot
Jan 6 at 15:24
I suggest you ask a question on codereview.meta.stackexchange.com/questions about this. Unless it is copying your code I don't think there is a rule here about copy and paste.
â pacmaninbw
Jan 6 at 15:32
@pacmaninbw I think the help center states it has to be code you own.
â Mast
Jan 6 at 15:52
@Antot it is my solution, the solution you have shared is different. it creates a new tree entirely where as this solution uses one of the trees in provided in the input.
â Abhiroj Panwar
Jan 6 at 16:42
 |Â
show 1 more comment
@Antot If you think it is a duplicate question, mark it as a duplicate.
â pacmaninbw
Jan 6 at 15:14
@pacmaninbw: it's not duplicate, it's about code that copy-pasted from elsewhere.
â Antot
Jan 6 at 15:24
I suggest you ask a question on codereview.meta.stackexchange.com/questions about this. Unless it is copying your code I don't think there is a rule here about copy and paste.
â pacmaninbw
Jan 6 at 15:32
@pacmaninbw I think the help center states it has to be code you own.
â Mast
Jan 6 at 15:52
@Antot it is my solution, the solution you have shared is different. it creates a new tree entirely where as this solution uses one of the trees in provided in the input.
â Abhiroj Panwar
Jan 6 at 16:42
@Antot If you think it is a duplicate question, mark it as a duplicate.
â pacmaninbw
Jan 6 at 15:14
@Antot If you think it is a duplicate question, mark it as a duplicate.
â pacmaninbw
Jan 6 at 15:14
@pacmaninbw: it's not duplicate, it's about code that copy-pasted from elsewhere.
â Antot
Jan 6 at 15:24
@pacmaninbw: it's not duplicate, it's about code that copy-pasted from elsewhere.
â Antot
Jan 6 at 15:24
I suggest you ask a question on codereview.meta.stackexchange.com/questions about this. Unless it is copying your code I don't think there is a rule here about copy and paste.
â pacmaninbw
Jan 6 at 15:32
I suggest you ask a question on codereview.meta.stackexchange.com/questions about this. Unless it is copying your code I don't think there is a rule here about copy and paste.
â pacmaninbw
Jan 6 at 15:32
@pacmaninbw I think the help center states it has to be code you own.
â Mast
Jan 6 at 15:52
@pacmaninbw I think the help center states it has to be code you own.
â Mast
Jan 6 at 15:52
@Antot it is my solution, the solution you have shared is different. it creates a new tree entirely where as this solution uses one of the trees in provided in the input.
â Abhiroj Panwar
Jan 6 at 16:42
@Antot it is my solution, the solution you have shared is different. it creates a new tree entirely where as this solution uses one of the trees in provided in the input.
â Abhiroj Panwar
Jan 6 at 16:42
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The time complexity of the algorithm is linear,
proportional to the number of nodes in the trees.
The worst case is when both trees have the same shape,
then all the nodes in both trees will be visited.
More generally, the number of nodes visited is proportional to min(size(t1), size(2))
.
I don't understand why the question is tagged with time-limit-exceeded, this posted solution should pass just fine.
A few minor improvements are possible.
- The
left
andright
local variables are redundant, you could assign directly to the fields oft1
- The statement
t2=null
is completely unnecessary - Spaces around operators (like
=
) would make the code easier to read
Like this:
if (t1 == null) return t2;
if (t2 == null) return t1;
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The time complexity of the algorithm is linear,
proportional to the number of nodes in the trees.
The worst case is when both trees have the same shape,
then all the nodes in both trees will be visited.
More generally, the number of nodes visited is proportional to min(size(t1), size(2))
.
I don't understand why the question is tagged with time-limit-exceeded, this posted solution should pass just fine.
A few minor improvements are possible.
- The
left
andright
local variables are redundant, you could assign directly to the fields oft1
- The statement
t2=null
is completely unnecessary - Spaces around operators (like
=
) would make the code easier to read
Like this:
if (t1 == null) return t2;
if (t2 == null) return t1;
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
add a comment |Â
up vote
1
down vote
accepted
The time complexity of the algorithm is linear,
proportional to the number of nodes in the trees.
The worst case is when both trees have the same shape,
then all the nodes in both trees will be visited.
More generally, the number of nodes visited is proportional to min(size(t1), size(2))
.
I don't understand why the question is tagged with time-limit-exceeded, this posted solution should pass just fine.
A few minor improvements are possible.
- The
left
andright
local variables are redundant, you could assign directly to the fields oft1
- The statement
t2=null
is completely unnecessary - Spaces around operators (like
=
) would make the code easier to read
Like this:
if (t1 == null) return t2;
if (t2 == null) return t1;
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The time complexity of the algorithm is linear,
proportional to the number of nodes in the trees.
The worst case is when both trees have the same shape,
then all the nodes in both trees will be visited.
More generally, the number of nodes visited is proportional to min(size(t1), size(2))
.
I don't understand why the question is tagged with time-limit-exceeded, this posted solution should pass just fine.
A few minor improvements are possible.
- The
left
andright
local variables are redundant, you could assign directly to the fields oft1
- The statement
t2=null
is completely unnecessary - Spaces around operators (like
=
) would make the code easier to read
Like this:
if (t1 == null) return t2;
if (t2 == null) return t1;
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
The time complexity of the algorithm is linear,
proportional to the number of nodes in the trees.
The worst case is when both trees have the same shape,
then all the nodes in both trees will be visited.
More generally, the number of nodes visited is proportional to min(size(t1), size(2))
.
I don't understand why the question is tagged with time-limit-exceeded, this posted solution should pass just fine.
A few minor improvements are possible.
- The
left
andright
local variables are redundant, you could assign directly to the fields oft1
- The statement
t2=null
is completely unnecessary - Spaces around operators (like
=
) would make the code easier to read
Like this:
if (t1 == null) return t2;
if (t2 == null) return t1;
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
answered Jan 6 at 17:48
janos
95.6k12120343
95.6k12120343
add a comment |Â
add a comment |Â
@Antot If you think it is a duplicate question, mark it as a duplicate.
â pacmaninbw
Jan 6 at 15:14
@pacmaninbw: it's not duplicate, it's about code that copy-pasted from elsewhere.
â Antot
Jan 6 at 15:24
I suggest you ask a question on codereview.meta.stackexchange.com/questions about this. Unless it is copying your code I don't think there is a rule here about copy and paste.
â pacmaninbw
Jan 6 at 15:32
@pacmaninbw I think the help center states it has to be code you own.
â Mast
Jan 6 at 15:52
@Antot it is my solution, the solution you have shared is different. it creates a new tree entirely where as this solution uses one of the trees in provided in the input.
â Abhiroj Panwar
Jan 6 at 16:42