What's the time complexity of this BFS alg that returns a 2D ArrayList? - binary-tree

I'm trying to figure out what time complexity this method would be to get better at computing time complexities. I was told by someone else that this would be a worst case O(n^2) (n being elements in the tree) but I'm confused since the outer while loop would only go the amount of levels, not total elements.
Any help would be great!
public List<List> returnLevelOrder(TreeNode root) {
<List<List> ret = new ArrayList<List> ();
Queue<Integer> queue = new List<Integer>();
if(root==null){
return ret;
}
queue.add(root);
while(!queue.empty()) {
ArrayList<Integer> level = new ArrayList<Integer>();
int levelSize = queue.size();
while(levelSize >0) {
TreeNode currNode = queue.poll();
level.add(currNode.val);
levelSize--;
if(currNode.left != null) {
queue.add(currNode.left.val);
}
if(currNode.right != null) {
queue.add(currNode.right.val);
}
}
ret.add(level);
}
return ret;
}

It is O(n) - you visit all the elements once - it is an iterative tree traversal.

Related

Space complexity of two recursive functions

I've got an interesting case doing LeetCode - I'm not sure what is Space complexity of this function. On the one hand when it calls itself, there are not traverse functions in the stack, so I believe space complexity is O(n).
But but if the recursion stack is empty should I say that this is not O(n^2) but O(n)?
Any help much appreciated!
class Solution {
int count = 0;
long targetSum;
public int pathSum(TreeNode root, int targetSum) {
if (root == null) return 0;
this.targetSum = targetSum;
traverse(root, (long)0);
pathSum(root.left, targetSum);
pathSum(root.right, targetSum);
return count;
}
private void traverse(TreeNode root, long currSum) {
if (root == null) return;
if (currSum + root.val == this.targetSum) {
this.count++;
}
traverse(root.left, currSum + root.val);
traverse(root.right, currSum + root.val);
}
}

SINGLE Recursive function for Print/Fetch kth smallest element in Binary search tree

I am trying to print the kth smallest element in an BST.
The first solution is using in-order traversal.
Next solution is finding the index of the current node by calculation the size of its left subtree.
Complete algo:
Find size of left subtree:
1.If size = k-1, return current node
2.If size>k return (size-k)th node in right subtree
3.If size<k return kth node in left subtree
This can be implemented using a separate count function which looks something like
public class Solution {
public int kthSmallest(TreeNode root, int k) {
//what happens if root == null
//what happens if k > total size of tree
return kthSmallestNode(root,k).val;
}
public static TreeNode kthSmallestNode(TreeNode root,int k){
if(root==null) return root;
int numberOfNodes = countNodes(root.left);
if(k == numberOfNodes ) return root;
if(k<numberOfNodes ) return kthSmallestNode(root.left,k);
else return kthSmallestNode(root.right,k-numberOfNodes );
}
private static int countNodes(TreeNode node){
if(node == null) return 0;
else return 1+countNodes(node.left)+countNodes(node.right);
}
}
But I see that we count the size for same trees multiple times, so one way is to maintain an array to store thes sizes like the DP way.
But I want to write a recursive solution for this.And here is the code I have written.
class Node {
int data;
Node left;
Node right;
public Node(int data, Node left, Node right) {
this.left = left;
this.data = data;
this.right = right;
}
}
public class KthInBST
{
public static Node createBST(int headData)
{
Node head = new Node(headData, null, null);
//System.out.println(head.data);
return head;
}
public static void insertIntoBst(Node head, int data)
{
Node newNode = new Node(data, null, null);
while(true) {
if (data > head.data) {
if (head.right == null) {
head.right = newNode;
break;
} else {
head = head.right;
}
} else {
if (head.left == null) {
head.left = newNode;
break;
} else {
head = head.left;
}
}
}
}
public static void main(String[] args)
{
Node head = createBST(5);
insertIntoBst(head, 7);
insertIntoBst(head, 6);
insertIntoBst(head, 2);
insertIntoBst(head, 1);
insertIntoBst(head, 21);
insertIntoBst(head, 11);
insertIntoBst(head, 14);
insertIntoBst(head, 3);
printKthElement(head, 3);
}
public static int printKthElement(Node head, int k)
{
if (head == null) {
return 0;
}
int leftIndex = printKthElement(head.left, k);
int index = leftIndex + 1;
if (index == k) {
System.out.println(head.data);
} else if (k > index) {
k = k - index;
printKthElement(head.right, k);
} else {
printKthElement(head.left, k);
}
return index;
}
}
This is printing the right answer but multiple times, I figured out why it is printing multiple times but not understanding how to avoid it.
And also If I want to return the node instead of just printing How do I do it?
Can anyone please help me with this?
Objective:
Recursively finding the kth smallest element in a binary search tree and returning the node corresponding to that element.
Observation:
The number of elements smaller than the current element is the size of the left subtree so instead of recursively calculating its size, we introduce a new member in class Node, that is, lsize which represents the size of the left subtree of current node.
Solution:
At each node we compare the size of left subtree with the current value of k:
if head.lsize + 1 == k: current node in our answer.
if head.lsize + 1 > k: elements in left subtree are more than k, that is, the k the smallest element lies in the left subtree. So, we go left.
if head.lsize + 1 < k: the current element alongwith all the elements in the left subtree are less than the kth element we need to find. So, we go to the right subtree but also reduce k by the amount of elements in left subtree + 1(current element). By subtracting this from k we make sure that we have already taken into account the number of elements which are smaller than k and are rooted as the left subtree of current node (including the current node itself).
Code:
class Node {
int data;
Node left;
Node right;
int lsize;
public Node(int data, Node left, Node right) {
this.left = left;
this.data = data;
this.right = right;
lsize = 0;
}
}
public static void insertIntoBst(Node head, int data) {
Node newNode = new Node(data, null, null);
while (true) {
if (data > head.data) {
if (head.right == null) {
head.right = newNode;
break;
} else {
head = head.right;
}
} else {
head.lsize++; //as we go left, size of left subtree rooted
//at current node will increase, hence the increment.
if (head.left == null) {
head.left = newNode;
break;
} else {
head = head.left;
}
}
}
}
public static Node printKthElement(Node head, int k) {
if (head == null) {
return null;
}
if (head.lsize + 1 == k) return head;
else if (head.lsize + 1 > k) return printKthElement(head.left, k);
return printKthElement(head.right, k - head.lsize - 1);
}
Changes:
A new member lsize has been introduced in class Node.
Slight modification in insertIntoBst.
Major changes in printKthElement.
Corner case:
Add a check to ensure that k is between 1 and the size of the tree otherwise a null node will be returned resulting in NullPointerException.
This is working on the test cases I have tried, so far. Any suggestions or corrections are most welcome.
:)

How do you find the nth node in a binary tree?

I want to find the nth node/element in a binary tree. Not the nth largest/smallest, just the nth in inorder order for example.
How would this be done? Is it possible to keep it to one function? Many functions employ an external variable to keep track of the iterations outside of the recursion, but that seems... lazy, for lack of a better term.
You can augment the binary search tree into an order statistic tree, which supports a "return the nth element" operation
Edit: If you just want the ith element of an inorder traversal (instead of the ith smallest element) and don't want to use external variables then you can do something like the following:
class Node {
Node left
Node right
int data
}
class IterationData {
int returnVal
int iterationCount
}
IterationData findNth(Node node, IterationData data, int n) {
if(node.left != null) {
data = findNth(node.left, data, n)
}
if(data.iterationCount < n) {
data.iterationCount++
if(data.iterationCount == n) {
data.returnVal = node.data
return data
} else if(node.right != null) {
return findNth(node.right, data, n)
} else {
return data
}
}
}
You'll need some way to return two values, one for the iteration count and one for the return value once the nth node is found; I've used a class, but if your tree contains integers then you could use an integer array with two elements instead.
In order iterative traversal, keep track of nodes passed in external variable.
public static Node inOrderInterativeGet(Node root, int which){
Stack<Node> stack = new Stack<Node>();
Node current = root;
boolean done = false;
int i = 1;
if(which <= 0){
return null;
}
while(!done){
if(current != null){
stack.push(current);
current = current.getLeft();
}
else{
if(stack.empty()){
done = true;
}
else{
current = stack.pop();
if(i == which){
return current;
}
i++;
current = current.getRight();
}
}
}
return null;
}
One way to do it is to have a size property which is left_subtree + right_subtree + 1:
class Node:
def __init__(self, data=None, left=None, right=None,
size=None):
self.data = data
self.left = left
self.right = right
self.size = size
def select(node, k):
"""Returns node with k-th position in-order traversal."""
if not node:
return None
t = node.left.size if node.left else 0
if t > k:
return select(node.left, k)
elif t < k:
return select(node.right, k - t - 1)
else:
return node
If you don't like global variable, pass to recursive function additional parameter - some int variable, let's call it auto_increment or just ai. ai stores order of current node. Also, recursive function should return maximal value of ai of current vertex subtree,because after visiting whole subtree next 'free' value will be max_ai_in_subreee+1 Something like that
int rec(int vertex,int ai){
traverseOrder[vertex] = ai
if(getChildren(vertex)!=null) return ai;
else{
for(childrens){
ai = rec(child,ai+1);
}
return ai;// subtree visited, return maximal free value upstairs.
}
}
If your function already returns some useful data, it may return some complex object which contains {useful data+ai}
Start from some vertex looks like rec(start_vertex,1);
Below is full code that you can use to find the nth element using inorder in a Binary Tree.
public class NthNodeInInoeder {
static public class Tree {
public int data;
public Tree left,right;
public Tree(int data) {
this.data = data;
}
}
static Tree root;
static int count = 0;
private static void inorder(Tree root2, int num) {
if (root2 == null)
return;
Tree node = root2;
Stack<Tree> stack = new Stack<>();
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.left;
}
node = stack.pop();
count++;
if (count == num) {
System.out.println(node.data);
break;
}
node = node.right;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
root = new Tree(10);
root.left = new Tree(20);
root.right = new Tree(30);
root.left.left = new Tree(40);
root.left.right = new Tree(50);
int num = sc.nextInt();
inorder(root, num);
sc.close();
}
}

Second max in BST

This is an interview question. Find the second max in BST.
The max element is the rightmost leaf in the BST. The second max is either its parent or its left child. So the solution is to traverse the BST to find the rightmost leaf and check its parent and left child.
Does it make sense?
No, that's wrong. Consider this BST:
137
/
42
\
99
Here, the second-to-max value is the rightmost child of the left child of the max value. Your algorithm will need to be updated so that you check the parent of the max value, or the rightmost subchild of the left child of the max.
Also, note that the max is not necessarily the rightmost leaf node, it's the node at the bottom of the right spine of the tree. Above, 137 is not a leaf.
Hope this helps!
Recall that you can list the nodes of a BST in reverse order by doing a modified inorder traversal where you explore the right subtree first. This leads to a simple algorithm:
Node rightmost = findRightmostNode(root)
if (rightmost.left != null) {
return findRightmostNode(rightmost.left)
else{
return rightmost.parent
}
It would return null if the tree has only one element.
public static int findSecondLargestValueInBST(Node root)
{
int secondMax;
Node pre = root;
Node cur = root;
while (cur.Right != null)
{
pre = cur;
cur = cur.Right;
}
if (cur.Left != null)
{
cur = cur.Left;
while (cur.Right != null)
cur = cur.Right;
secondMax = cur.Value;
}
else
{
if (cur == root && pre == root)
//Only one node in BST
secondMax = int.MinValue;
else
secondMax = pre.Value;
}
return secondMax;
}
Much easier iterative approach with Time complexity O(logN) and Space complexity O(1)
public static void main(String[] args) {
BinaryTreeNode result=isBinarySearchTree.secondLargest(rootNode);
System.out.println(result.data);
}
private BinaryTreeNode secondLargest(BinaryTreeNode node) {
BinaryTreeNode prevNode=null; //2nd largest Element
BinaryTreeNode currNode=node;
if(null == currNode)
return prevNode;
while(currNode.right != null){
prevNode=currNode;
currNode=currNode.right;
}
if(currNode.left != null){
currNode=currNode.left;
while(currNode.right != null){
currNode=currNode.right;
}
prevNode=currNode;
}
return prevNode;
}
The algo can be as follows
1. find the largest number in the tree.
private static int findLargestValueInTree(Node root) {
while (root.right != null) {
root = root.right;
}
return root.data;
}
2. Find the largest number in the tree that is smaller than the number we found in step 1
public static int findSecondLargest(Node root, int largest, int current) {
while (root != null) {
if (root.data < largest) {
current = root.data;
root = root.right;
} else {
root = root.left;
}
}
return current;
}
'current' keeps track of the current largest number which is smaller than the number found in step1
Simple javascript implementation.
function Node (value, left, right) {
this.value = value;
this.left = left;
this.right = right;
}
function second (node, prev, wentLeft) {
if (node.right) {
return second(node.right, node, wentLeft);
} else if (node.left) {
return second(node.left, node, true);
} else {
if (wentLeft) return node.value;
return prev.value;
}
}
second(root);
One traversal variant:
public Tree GetSecondMax(Tree root)
{
Tree parentOfMax = null;
var maxNode = GetMaxNode(root, ref parentOfMax);
if (maxNode == root || maxnode.left != null)
{
// if maximum node is root or have left subtree, then return maximum from left subtree
return GetMaxNode(maxnode.left, ref parentOfMax);
}
// if maximum node is not root, then return parent of maximum node
return parentOfMax;
}
private Tree GetMaxNode(Tree root, ref Tree previousNode)
{
if (root == null || root.right == null)
{
// The most right element have reached
return root;
}
// we was there
previousNode = root;
return GetMaxNode(root.right, ref previousNode);
}
int getmax(node *root)
{
if(root->right == NULL)
{
return root->d;
}
return getmax(root->right);
}
int secondmax(node *root)
{
if(root == NULL)
{
return -1;
}
if(root->right == NULL && root->left != NULL)
{
return getmax(root->left);
}
if(root->right != NULL)
{
if(root->right->right == NULL && root->right->left == NULL)
{
return root->d;
}
}
return secondmax(root->right);
}
A very intuitive way to think about this is considering the following two cases.
Let second largest Node as S, and largest node as L.
i) S is inserted to the BST "earlier" than L.
ii) S is inserted to the BST "later" than L.
For the first case, it is obvious that L is the right child of S. It is because any node except for L is smaller than S, therefore will not be put on the right side of S. Therefore when L is being put, it will be the right child of S.
For the second case, by the time when S is inserted, L will the be right most node in the BST. Obviously, L wouldn't have a right child because it is the largest. However, L could have its own left subtree. When S is inserted, S will follow to "right path" until it meets L and then turn left to go to the left subtree of L. Here, we know that all of the nodes in L's left subtree are smaller than S, so S will be the rightmost node in the subtree.
int getSecondLargest(Node root){
if(root==null)
return 0;
Node curr=root;
Node prev=root;
//Go to the largest node
while(curr.right != null){
prev = curr;
curr= curr.right;
}
//If largest Node has left child, Then largest element of tree with its root as largest.left will be the second largest number.
if(curr.left == null)
return prev.data;
else
return findLargest(curr.left);
}
int findLargest(Node root){
// No need toi check for null condition as in getSecondLargest method, its already done.
Node curr=root;
//To find largest just keep on going to right child till leaf is encountered.
while(curr.right != null){
curr = curr.right;
}
return curr.data;
}
I would do it by going though the tree from biggest to smallest element and returning value when asked position is reached. I implemented similar task for second largest value.
void BTree::findSecondLargestValueUtil(Node* r, int &c, int &v)
{
if(r->right) {
this->findSecondLargestValueUtil(r->right, c, v);
}
c++;
if(c==2) {
v = r->value;
return;
}
if(r->left) {
this->findSecondLargestValueUtil(r->left, c, v);
}
}
int BTree::findSecondLargestValue()
{
int c = 0;
int v = -1;
this->findSecondLargestValueUtil(this->root, c, v);
return v;
}
You are close to the right answer.
Here is my attempt at an intuitive answer.
The greatest node is the right most node.
Whatever is under the right most node's left sub-tree is greater than all elements except the right most node. Therefore the greatest node in this sub-tree is the answer.
If there is no left sub-tree then the parent of the right most node is the one that is greater than all the other nodes except the right most node.
The idea is to go all the way to the right until there is nothing on the right. If there's a left, take it and then go all the way to the right. If you took a left, the answer is the last node encountered. Otherwise the answer is the second-last node you encountered.
Here's a recursive solution in Java:
public TreeNode getSecondLargest(TreeNode root) {
if(root == null || (root.left == null && root.right == null))
throw new IllegalArgumentException("The tree must have at least two nodes");
return helper(root, null, false);
}
private TreeNode helper(TreeNode root, TreeNode parent, boolean wentLeft) {
if(root.right != null) return helper(root.right, root, wentLeft);
if(root.left != null && !wentLeft) return helper(root.left, root, true);
if(wentLeft) return root;
else return parent;
}
The time complexity is O(lg n).
This Algorithm to find 2nd Max Element in a BST , will take Time Complexity: O(n) --> in worst case where tree is a right skew tree.
And if the Tree is balanced Tree then the Time Complexity is O(height)
ie O(log n)
And Same Goes with Space Complexity as well: O(n) --> in worst case
O(log n) --> when Tree is balanced.
public TreeNode secondMax(TreeNode root){
return helper(root,null);
}
private TreeNode helper(TreeNode root,TreeNode prev){
if(root==null)
return root;
if(root.right==null && root.left!=null)
return root.left;
else if(root.right==null && root.left==null)
return prev;
return helper(root.right,root);
}
This algorithm does one run on the tree and returns the largest item at Item1 and second largest at Item2.
The sort calls are O(1) because they are independent of the tree size.
So the total time complexity is O(N) and space complexity is O(log(N)) when the tree is balanced.
public static Tuple<int, int> SecondLargest(TreeNode<int> node)
{
int thisValue = node.Value;
if ((node.Left == null || node.Left.Right == null) && node.Right == null)
{
return new Tuple<int, int>(thisValue, -int.MaxValue);
}
else if (node.Left == null || node.Left.Right == null)
{
Tuple<int, int> right = SecondLargest(node.Right);
List<int> sortLargest = new List<int>(new int[] { right.Item1, right.Item2, thisValue });
sortLargest.Sort();
return new Tuple<int, int>(sortLargest[2], sortLargest[1]);
}
else if (node.Right == null)
{
Tuple<int, int> left = SecondLargest(node.Left.Right);
List<int> sortLargest = new List<int>(new int[] { left.Item1, left.Item2, thisValue });
sortLargest.Sort();
return new Tuple<int, int>(sortLargest[2], sortLargest[1]);
}
else
{
Tuple<int, int> left = SecondLargest(node.Left.Right);
Tuple<int, int> right = SecondLargest(node.Right);
List<int> sortLargest = new List<int>(new int[] { left.Item1, left.Item2, right.Item1, right.Item2, thisValue });
sortLargest.Sort();
return new Tuple<int, int>(sortLargest[4], sortLargest[3]);
}
}

How to traverse this tree in reverse order

I found an implementation of the Morris tree traversal,
It works fine,,, but having a bit of a problem trying
to traverse the tree in reverse order.. -
void MorrisTraversal(struct Node *root)
{
struct Node *p,*pre;
if(root==0) { return; }
for(p=root;p!=0;)
{
if(p->Left==0) { printf(" %d ",p->Data); p=p->Right; continue; }
for(pre=p->Left;pre->Right!=0 && pre->Right!=p;pre=pre->Right) { }
if(pre->Right==0)
{ pre->Right=p; p=p->Left; continue; }
else
{ pre->Right=0; printf(" %d ",p->Data); p=p->Right; continue; }
}
}
By reverse order, I'm assuming you mean reverse inorder traversal. You have at least two options:
You could modify the code and swap all the ->Right pointer references with ->Left ones.
You could replace the two printf statements with pushes onto a stack. Once the algorithm completes, you would then pop off the data from the stack to print them. This however defeats the whole purpose of the Morris traversal algorithm, which was to be stackless.
This related SO thread might help you understanding.
I hope this helps.
Here is one sample in Java. Click Here for one more sample using iteration instead of recursion.
public int depthOfTree(TreeNode node){
if(node == null){
return 0;
}
int leftDepth = depthOfTree(node.left);
int rightDepth = depthOfTree(node.right);
if(leftDepth > rightDepth){
return leftDepth+1;
} else {
return rightDepth+1;
}
}
//Reverse Level with recursion
public void reverseLevelOrder(TreeNode node){
int depth = depthOfTree(node);
for(int i=depth;i>0;i--){
printTree(node,i);
}
}
public void printTree(TreeNode node, int level){
if(node == null){
return;
}
if(level == 1){
System.out.print(node.data+" ,");
} else if(level>1){
printTree(node.left, level-1);
printTree(node.right, level-1);
}
}

Resources