[LeetCode]113 Binary Tree Postorder Traversal

https://oj.leetcode.com/problems/path-sum-ii/

http://fisherlei.blogspot.com/search?q=Path+Sum+II+

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        
        // 需要遍历树
        // 对于每个节点,记录从root到此节点(包含)的路径值和路径,可以用map来储存
        // 当遇到leaf,且符合条件,加入到result
        
        List<List<Integer>> result = new ArrayList<>();
        if (root == null)
            return result;
            
        Map<TreeNode, Path> pathmap = new HashMap<>();
        pathmap.put(root, Path.append(new Path(), root));
        
        find(root, sum, pathmap, result);
        
        return result;
    }
    
    private void find(TreeNode node, int target, Map<TreeNode, Path> pathmap, List<List<Integer>> result)
    {
        Path path = pathmap.get(node);
        
        if (node.left == null && node.right == null)
        {
            // A leaf
            if (path.sum == target)
            {
                result.add(path.path);
            }
            return;
        }
        
        if (node.left != null)
        {
            Path leftpath = Path.append(path, node.left);
            pathmap.put(node.left, leftpath);
            find(node.left, target, pathmap, result);
        }
        
        if (node.right != null)
        {
            Path rightpath = Path.append(path, node.right);
            pathmap.put(node.right, rightpath);
            find(node.right, target, pathmap, result);
        }
    }
    
    private static class Path
    {
        int sum;
        List<Integer> path;
        
        Path()
        {
            sum = 0;
            path = new ArrayList();
        }
        
        static Path append(Path p, TreeNode node)
        {
            Path toReturn = new Path();
            toReturn.sum = p.sum + node.val;
            toReturn.path = new ArrayList<Integer>(p.path);
            toReturn.path.add(node.val);
            return toReturn;
        }
    }
}
时间: 2024-12-14 18:08:15

[LeetCode]113 Binary Tree Postorder Traversal的相关文章

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

[LeetCode][JavaScript]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 同样的配方,同样的味道.

【LeetCode】Binary Tree Postorder Traversal (3 solutions)

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归法 /** *

Leetcode dfs Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Total Accepted: 28560 Total Submissions: 92333My Submissions Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive s

Java for LeetCode 145 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. 后序遍历,左子树→右子树→根节点 前序遍历的非递归实现需要一个计数器,方法是需要重写一个类继承TreeNode,翁慧玉教材<数据结构:题解与拓展>P113有详细介绍,这里略.递归JAVA实现如下: public Lis

leetcode - [6]Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. 思路:后序遍历是按照“左子树,右子树,根”的顺序访问元素.那么根或者其它父亲元素就要先压入栈,然后再弹出. #include <iostream> #include <algorithm> #include