首页 > 其他 > 详细

Path Sum

时间:2015-03-27 19:59:05      阅读:252      评论:0      收藏:0      [点我收藏+]

问题来源:https://leetcode.com/problems/path-sum/

package cn.edu.shu;

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

/**
 * 
 * <p>
 * ClassName PathSum
 * </p>
 * <p>
 * Description 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.
 * </p>
 * 
 * @author TKPad wangx89@126.com
 *         <p>
 *         Date 2015年3月27日 下午3:07:37
 *         </p>
 * @version V1.0.0
 *
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

// 这是比较笨的一种方法,思路主要是先使用深度优先遍历,获取到每一个到叶子节点的完整路径,然后计算该条完整路径上的节点的和
public class PathSum {
    ArrayList<Integer> list = new ArrayList<Integer>();
    List<ArrayList<Integer>> listAll = new ArrayList<ArrayList<Integer>>();

    public boolean hasPathSum(TreeNode root, int sum) {
        depthFirst(root);
        Iterator<ArrayList<Integer>> iterator = listAll.iterator();
        while (iterator.hasNext()) {
            ArrayList<Integer> next = iterator.next();
            Iterator<Integer> iterator2 = next.iterator();
            int result = 0;
            while (iterator2.hasNext()) {
                Integer next2 = iterator2.next();
                result += next2;
            }
            if (result == sum) {
                return true;
            }
        }
        return false;
    }

    public TreeNode depthFirst(TreeNode root) {
        if (null == root) {
            return null;
        }
        list.add(root.val);
        if (root.left == null && root.right == null) {
            // 以list构造一个新的对象,这样可以将其加入List集合,而不会更改到原引用的值
            ArrayList<Integer> temp = new ArrayList<Integer>(list);
            listAll.add(temp);
        }
        if (root.left != null) {
            depthFirst(root.left);
            list.remove(list.size() - 1);
        }

        if (root.right != null) {
            depthFirst(root.right);
            list.remove(list.size() - 1);
        }
        return root;
    }

    public static void main(String[] args) {
        TreeNode tn1 = new TreeNode(5);
        TreeNode tn2 = new TreeNode(4);
        TreeNode tn3 = new TreeNode(8);
        TreeNode tn4 = new TreeNode(11);
        TreeNode tn5 = new TreeNode(13);
        TreeNode tn6 = new TreeNode(4);
        TreeNode tn7 = new TreeNode(7);
        TreeNode tn8 = new TreeNode(2);
        TreeNode tn9 = new TreeNode(1);
        tn1.left = tn2;
        tn1.right = tn3;
        tn2.left = tn4;
        tn4.left = tn7;
        tn4.right = tn8;
        tn3.left = tn5;
        tn3.right = tn6;
        tn6.right = tn9;
        boolean hasPathSum = new PathSum().hasPathSum(null, 22);
        System.out.println(hasPathSum);
    }
}

Path Sum

原文:http://blog.csdn.net/shijiebei2009/article/details/44680855

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!