LeetCode112 Path Sum

详细见:leetcode.com/problems/path-sum


Java Solution: github

package leetcode;

import java.util.LinkedList;
import java.util.Queue;

import tools.TreeNode辅助.TreeNode;

/*
 * 	Given a binary tree and a sum, determine if the tree has a root-to-leaf
 *  path such that adding up all the values along the path equals the given sum.

	For example:
	Given the below binary tree and sum = 22,
	              5
	             / \
	            4   8
	           /   / \
	          11  13  4
	         /  \      \
	        7    2      1
	return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
 */

public class P112_PathSum {
	public static void main(String[] args) {
		
	}
	/*
	 * 	AC
	 * 	4 ms
	 */
	static class Solution {
	    public boolean hasPathSum(TreeNode root, int sum) {
	    	if (root == null) {
	    		return false;
	    	}
	    	boolean isDone = false;
	    	Queue<TreeNode> q1 = new LinkedList<>();
	    	Queue<Integer> q2 = new LinkedList<>();
	    	q1.add(root);
	    	q2.add(root.val);
	    	int depth_now = 1;
	    	while (! q1.isEmpty()) {
	    		TreeNode root_now = q1.poll();
	    		depth_now = q2.poll();
	    		if (root_now.left == null && root_now.right == null && depth_now == sum) {
	    			isDone = true;
	    			break;
	    		}
	    		if (root_now.left != null) {
	    			q1.add(root_now.left);
	    			q2.add(depth_now + root_now.left.val);
	    		}
	    		if (root_now.right != null) {
	    			q1.add(root_now.right);
	    			q2.add(depth_now + root_now.right.val);
	    		}
	    	}
	        return isDone;
	    }
	}
}


C Solution: github

/*
    url: leetcode.com/problems/path-sum
    AC 6ms 27.83%
*/

#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

typedef int bool;

typedef struct TreeNode stn;
typedef struct TreeNode * ptn;


ptn tn_init(int val) {
    ptn n = (ptn) malloc(sizeof(stn));
    n->val = val;
    n->left = NULL;
    n->right = NULL;
    return n;
}

typedef ptn T;
typedef struct al sal;
typedef struct al * pal;

struct al {
    int capacity;
    int size;
    T* arr;
};

pal al_init(int capacity) {
    pal l = (pal) malloc(sizeof(sal));
    if (capacity < 1) return NULL;
    l->arr = (T*) malloc(sizeof(T) * capacity);
    l->capacity = capacity;
    l->size = 0;
    return l;
}

void al_expand_capacity(pal l) {
    T* new_arr = (T*) malloc(sizeof(T) * (l->capacity * 2 + 1));
    int i = 0;
    for (i = 0; i < l->capacity; i ++)
        new_arr[i] = l->arr[i];
    free(l->arr);
    l->arr = new_arr;
    l->capacity = l->capacity * 2 + 1;
}

void al_add_last(pal l, T v) {
    if (l->capacity == l->size) al_expand_capacity(l);
    l->arr[l->size] = v;
    l->size ++;
}

void al_free_all(pal l) {
    free(l->arr);
    free(l);
}

void swap_al(pal* l1, pal* l2) {
    pal t = *l1;
    *l1 = *l2;
    *l2 = t;
}

void search(ptn n, int c, int s, int* sign) {
    if (*sign) return;
    if (n == NULL) return;
    search(n->left, c+n->val, s, sign);
    search(n->right, c+n->val, s, sign);
    if (n->left == NULL && n->right == NULL && c+n->val == s) *sign=1;
}

bool hasPathSum(ptn n, int s) {
    int c = 0, sign = 0;
    search(n, 0, s, &sign);
    return sign;
}


Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/path-sum
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年4月30日
    @details:    Solution: 79ms 46.08%
'''

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def search(self, n, s, a):
        if n == None: return False
        if n.left == None and n.right == None:
            if s == a+n.val: return True
        return self.search(n.left, s, a+n.val) \
            or self.search(n.right, s, a+n.val)
        
    def hasPathSum(self, n, s):
        """
        :type n: TreeNode
        :type s: int
        :rtype: bool
        """
        return self.search(n, s, 0)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值