Count Complete Tree Nodes | LeetCode 222 | C++, Java, Python

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

June LeetCoding Challenge | Problem 23 | Count Complete Tree Nodes | 23 June,
Facebook Coding Interview question,
google coding interview question,
leetcode,
Count Complete Tree Nodes,
Count Complete Tree Nodes c++,
Count Complete Tree Nodes Java,
Count Complete Tree Nodes python,
Count Complete Tree Nodes solution,
222. Count Complete Tree Nodes,

#Facebook #CodingInterview #LeetCode #JuneLeetCodingChallenge #Google #Amazon #CompleteBinaryTree
Рекомендации по теме
Комментарии
Автор

Time complexity?
Interested to know which software do u use to explain and write on

rashmikiranpandit
Автор

Please talk about time complexity after the code complete.

rmadhavmca
Автор

Loved the optimization part.I thought it is just a simple traversal problem.

manpreetkhokhar
Автор

The simple recursive solution : as follows
If(root==null) return 0;
Return 1+countNodes (root.left)+countNodes (root.right);

praveenj
Автор

Thanks for the explanation. It nice and easy to understand.
Can you tell me the software you use to create these videos?

kedaramnaik
Автор

*A faster approach*

_As you don't require two traverse twice in every recursive call_

public class Solution {
public int CountNodes(TreeNode root) {
if(root == null)
return 0;

TreeNode leftNode = root, rightNode = root;
int height = 0;

while(rightNode != null) {
height++;
rightNode = rightNode.right;
leftNode = leftNode.left;
}

if(leftNode == null)
return (1 << height) - 1;

return 1 + CountNodes(root.left) + CountNodes(root.right);
}
}

subham-raj
welcome to shbcf.ru