Node Depth in Binary Tree — Competitive Programming with Time and Space Complexity


Sample Input:

Binary Tree

Sample Output:


private int GetDepth(BinaryTree tree, int depth)
if (tree == null) return 0;
return depth + GetDepth(tree.left, depth + 1) +
GetDepth(tree.right, depth + 1);
private int NodeDepth(BinaryTree tree, int depth, int sum)
// Intially depth is 0 ans um is 0
if (tree.left != null)
sum = depth + NodeDepth(tree.left, depth, sum);
if (tree.right != null)
sum = depth + NodeDepth(tree.right, depth, sum);
return sum;




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

YieldShield Updates: When Public Beta?

Obviously, some decisions demand more thought than others. Life isn’t

Getting started with Embedded Systems — 2: Microcontrollers

Merge sort explained.

How to connect your Python AWS Lambda to a secure Elasticache Redis

What is a Business rule management system?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Wrestling

Code Wrestling

More from Medium

Substring with Concatenation of All Words — LeetCode

Longest Consecutive Sequence

𝐀 𝐧𝐞𝐰 𝐣𝐨𝐮𝐫𝐧𝐞𝐲 𝐰𝐢𝐭𝐡 𝐃𝐚𝐭𝐚 𝐒𝐭𝐫𝐮𝐜𝐭𝐮𝐫𝐞 𝐚𝐧𝐝 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦𝐬

Understanding time and space complexity with Big O