您的位置:首页 > 其它

LeetCode112 Path Sum

2017-05-01 15:10 465 查看
详细见: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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode