Google Interview Question for SDE1s

Country: United States

Comment hidden because of low score. Click to expand.
of 0 vote

import java.util.ArrayList;
import java.util.List;

* Created by zviad on 12/15/17.
public class TreeNodes {

private static class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode parent;
public TreeNode(int val) {
this.val = val;

public String toString() {
return "TreeNode{" +
"val=" + val +

", parent=" + parent +

int pathSum(TreeNode root,int sum){
List<TreeNode> leafNode=new ArrayList<>();
final int result[]={0};
if(checkPathSum(item,sum)) {
return result[0];

private Boolean checkPathSum(TreeNode treeNode ,int checksum){
return true;
}else if(treeNode.parent==null){
return false;
return checkPathSum(treeNode.parent,checksum-treeNode.val);

private void findeLeafs(List<TreeNode> leafNodes,TreeNode root){
if(root.left==null && root.right==null){

public static void main(String argv[]){
TreeNodes treeNodes=new TreeNodes();
TreeNode treenode=new TreeNode(8);
treenode.left=new TreeNode(3);
treenode.right=new TreeNode(2);
treenode.left.left=new TreeNode(26);
treenode.left.right=new TreeNode(23);
treenode.right.left=new TreeNode(1);
treenode.right.left.left=new TreeNode(12);


- zvnemsadze December 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

map<long, int> search(TreeNode *root){
    if (root == NULL){
        return map<long,int>();
    if (root->left == NULL && root->right == NULL){
        map<long, int> mp;
        mp[root->val] = 1;
        return mp;
    map<long, int> l = search(root->left);
    map<long, int> r = search(root->right);
    map<long, int> ret;
    if (l.size() != 0){
        for (auto it = l.begin(); it != l.end(); ++it){
            ret[it->first + root->val] += it->second;
    if (r.size() != 0){
        for (auto it = r.begin(); it != r.end(); ++it){
            ret[it->first + root->val] += it->second;
    return ret;

int pathSum(TreeNode *root, int target){
    map<long, int> mp = search(root);
    return mp[target];

- lxfuhuo January 07, 2018 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More