@Mr.Bin Hi, I think the time complexity is O(n) where h is the height of the tree.
Here is why, take the worst case where this tree only has one side. When we pass in the root node, it recursively reach down to the leaf of the tree. When pass in the leaf of the tree, we populate the result list, and return a value to its parent. And then its parent get put in the result list and return a value to the node above it. So actually we only touch each node once. So O(n).
Please correct me if I'm wrong.
xiangyu6
@xiangyu6
Posts made by xiangyu6

RE: 10 lines simple Java solution using recursion with explanation

RE: Very simple DFS solution
@IanWalker0421
I think it should be 3^3, since if the first three section is determined, the number value for the last section is fixed. But either way, it is constant. 
RE: Java(12ms)  left boundary, left leaves, right leaves, right boundary
@ctfu the problem says no duplicate nodes, not no duplicate values.

RE: 7line JavaScript solution using preorder DFS
@loctn
First thing that popped into my head is using BFS. And it is easy to implement. Read your code cause I am curious about how to do it with DFS.
There is one line I don't quite understand. Line 3:counts[level] = counts[level]  { sum: 0, nodes: 0 };
What does it do when you have an "or" operation on two objects?

RE: Javascript how to resolve 'Time Limit Exceeded ' ?
I tried your code, still TLE...

Can anybody tell me why this wouldn't work? #javascript
Can anybody tell me why this wouldn't work?
var sortedListToBST = function(head) { var root = help(head,null); return root }; var help = function(head,end){ if(head==end)return null var fast = head; var slow = head; while(fast!==end && fast.next!==end){ fast=fast.next.next; slow=slow.next; } var root = new ListNode(slow.val); root.left=help(head,slow); root.right=help(slow.next,end); return root }

javascript solution not working, don't know why...
Copied the solution from the java most voted solution. However it is not working in javascript. Can anybody explain why?
var combinationSum = function(candidates, target) { var list = []; candidates.sort(function(a,b){return ab}); backtrack(list, [],candidates,target,0); return list }; var backtrack = function(list, tempArray, candidates, remain, start){ if(remain<0){return} else if(remain===0){ list.push(tempArray); } else{ for(var i=start; i<candidates.length; i++){ tempArray.push(candidates[i]); backtrack(list,tempArray,candidates,remaincandidates[i],i); tempArray.pop(); } } }

RE: Why doesn't it work with JavaScript?
I had the same issue. I submitted my solution it says when the input was [], my out put was 3, which does not make any sense. I guess your suspicion about global variable is right. So I changed my code to the following, and it passed.
var diameterOfBinaryTree = function(root) { var max = 0; var helper = function(root){ if (root===null) return 0 var left = helper(root.left); var right = helper(root.right); max = Math.max(max, left + right); return Math.max(left, right) + 1; } helper(root); return max }

RE: javascript solution 132 ms
I am just curious, is str.replace() considered a low performance function?