leetcode112

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution
    {
        Stack<TreeNode> S = new Stack<TreeNode>();
        List<List<TreeNode>> list = new List<List<TreeNode>>();
        private void postNode(TreeNode node)
        {
            if (node != null)
            {
                S.Push(node);

                if (node.left != null)
                {
                    postNode(node.left);
                }

                if (node.right != null)
                {
                    postNode(node.right);
                }

                if (node.left == null && node.right == null)
                {
                    list.Add(S.ToList());
                }
                S.Pop();
            }
        }

        public bool HasPathSum(TreeNode root, int sum)
        {
            postNode(root);

            foreach (var l in list)
            {
                var psum = 0;
                foreach (var d in l)
                {
                    psum += d.val;
                }
                if (psum == sum)
                {
                    return true;
                }
            }

            return false;
        }
    }

https://leetcode.com/problems/path-sum/#/description

时间: 2024-10-24 13:23:31

leetcode112的相关文章

LeetCode112 Path Sum

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 t

LeetCode112——path sum

看到树就有一种本能的畏惧感,水平不足嘛.但是水平不够就越要敢于挑战,本题的意思是找到一条路径,这条路径上的和等于给出的数.这条路径必须是从叶子到根节点的哈.想想,如果从根节点开始找,每次sum减掉val则到了叶子节点的时候,sum肯定等于叶子节点的值.否则,就没有这么一条路径.朴素深搜,好装逼的词!! /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNo

LeetCode112:Path Sum

正常写法 bool HasPathSum(TreeNode root, int sum) { bool ret=false; if(root==null)return false; if(root.left==null&&root.right==null) return root.val==sum; if(root.left!=null) { ret=ret|| HasPathSum(root.left,sum-root.val); } if(root.right!=null) { ret

LeetCode112 PathSum Java题解

题目: 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,

DFS回溯只在递归基回溯————leetcode112

# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # class Solution: # def hasPathSum(self, root, sum): # """ # :type root: TreeNode # :type sum: int # :rtyp

第一个缺失数字

Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 思路: 桶排的思想 从头到尾遍历数组,在位置 i,希望放置的元素是 i+1, 如果不是, 就把 A[ i