private static void PreOrder(LeetCode1022TreeNode root, int currSum) { if (root == null) { return; } //visit the root; currSum = (currSum << 1) | root.val; // base case if (root.left == null && root.right == null) { ans += currSum; } // visit the left PreOrder(root.left, currSum); //visit the right; PreOrder(root.right, currSum); }
public LeetCode1022TreeNode(int val = 0, LeetCode1022TreeNode left = null, LeetCode1022TreeNode right = null) { this.val = val; this.left = left; this.right = right; }
public static int SumRootToLeafRecursive(LeetCode1022TreeNode root) { PreOrder(root, 0); return(ans); }