Set left node count
/**
* public class TreeNodeLeft {
* public int key;
* public TreeNodeLeft left;
* public TreeNodeLeft right;
* public int numNodesLeft;
* public TreeNodeLeft(int key) {
* this.key = key;
* }
* }
*/
public class Solution {
public void numNodesLeft(TreeNodeLeft root) {
numLeft(root);
}
public int numLeft(TreeNodeLeft root){
if (root == null) return 0;
int leftCount = numLeft(root.left);
int rightCount = numLeft(root.right);
root.numNodesLeft = leftCount;
return leftCount + rightCount + 1;
}
}Last updated