I am bit confused, what will be the time complexity of this algorithm, this is the answer for 366. that I put together, and its working fine; is it O(n*logn)?
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> result = new LinkedList<>();
if (root == null) return result;
while (root != null) {
ArrayList<Integer> res = new ArrayList<>();
root = findLeaves(root, res);
result.add(res);
}
return result;
}
private TreeNode findLeaves(TreeNode root, List<Integer> result) {
if (root == null) return null;
// add the leave node to the result
if (root.left == null && root.right == null) {
result.add(root.val);
return null; // indicate the parent, for leave node & stop
}
if (root.left != null)
root.left = findLeaves(root.left, result);
if (root.right != null)
root.right = findLeaves(root.right, result);
return root;
}
}