Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- constructor(value, left = null, right = null) {
- this.value = value;
- this.left = left;
- this.right = right;
- }
- }
- // 40
- // / \
- // 20 60
- // / \ / \
- // 10 30 50 70
- // Result: 4
- function getLeafCountOfBinaryTree(node) {
- if(node === null) return 0;
- if(node.left === null && node.right === null) return 1;
- return getLeafCountOfBinaryTree(node.left) + getLeafCountOfBinaryTree(node.right);
- }
- let rootNode =new Node(40);
- let node20=new Node(20);
- let node10=new Node(10);
- let node30=new Node(30);
- let node60=new Node(60);
- let node50=new Node(50);
- let node70=new Node(70);
- rootNode.left=node20;
- rootNode.right=node60;
- node20.left=node10;
- node20.right=node30;
- node60.left=node50;
- node60.right=node70;
- console.log(getLeafCountOfBinaryTree(rootNode));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement