二叉树的路径及其变形问题

二叉树中和为某一值的路径

http://www.nowcoder.com/questionTerminal/b736e784e3e34731af99065031301bca

在做这一题之前,我们先来看看怎么输出二叉树的从根结点到每个叶子节点的路径。

如:

        1

    /        \

2            3

/\            /

4 5 6

则返回 [[1, 2, 4], [1, 2, 5], [1, 3, 6]],其实就是深度优先遍历。

# 递归解法
class TreeNode:
    def __init__(self, value):
        self.val = value
        self.left = None
        self.right = None

class Solution:
    def binaryTreePaths(self, root):
        if root == None:
            return []
        result = []
        self.DFS(root, result, [root.val])
        return result

    def DFS(self, root, result, path):
        if root.left == None and root.right == None:
            result.append(path)
        if root.left != None:
            self.DFS(root.left, result, path + [root.left.val])
        if root.right != None:
            self.DFS(root.right, result, path + [root.right.val])
# 非递归解法

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

class Solution2:
    def binaryTreePaths2(self, root):
        if root == None:
            return []
        result = []
        stack = []
        stack.append((root, [root.val]))
        while stack:
            node, path = stack.pop()
            if node.left == None and node.right == None:
                result.append(path)
            if node.left != None:
                stack.append((node.left, path + [node.left.val]))
            if node.right != None:
                stack.append((node.right, path + [node.right.val]))
        return result

现在再来看这一题,就会发现:其实这一题就是引出所有的从根结点到叶子结点的路径的变形,就在判断条件上多了一个 sum(path) == self.sums。所以,解法如下:

# 方法一:递归
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    # 返回二维列表,内部每个列表表示找到的路径
    def FindPath(self, root, expectNumber):
        # write code here
        result = []
        if root == None:
            return result
        self.sums = expectNumber
        self.DFS(root, result, [root.val])
        return result

    def DFS(self, root, result, path):
        if root.left == None and root.right == None and sum(path) == self.sums:
            result.append(path)
        if root.left != None:
            self.DFS(root.left, result, path+[root.left.val])
# 方法二:非递归
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    # 返回二维列表,内部每个列表表示找到的路径
    def FindPath(self, root, expectNumber):
        # write code here
        if root == None:
            return []
        result = []
        stack = []
        stack.append((root, [root.val]))
        while stack:
            node, path = stack.pop()
            if node.left == None and node.right == None and sum(path) == expectNumber:
                result.append(path)
            if node.right != None:
                stack.append((node.right, path + [node.right.val]))
            if node.left != None:
                stack.append((node.left, path + [node.left.val]))
        return result
全部评论
请问一下怎么保证,返回值的list中,数组长度大的数组靠前?
3 回复
分享
发布于 2019-09-06 10:54
给返回的result排下序: result.sort(key = lambda i:len(i),reverse=True)
1 回复
分享
发布于 2019-11-01 11:11
滴滴
校招火热招聘中
官网直投
你这题递归解决方法中最后是不是掉了两句话: if root.right != None: self.DFS(root.left, result, path+[root.right.val])
1 回复
分享
发布于 2020-02-19 20:46
递归解法错了 加了两句话 if root.right != None: self.DFS(root.right, result, path+[root.right.val])
1 回复
分享
发布于 2020-03-29 14:53
非递归用的是广度优先遍历吧? 所以才要最后进行排序
点赞 回复
分享
发布于 2020-04-02 22:04
这个非递归的思路厉害,排序部分也巧妙,膜拜
点赞 回复
分享
发布于 2020-04-07 23:23
为什么非递归方式,先判断左孩子再判断右孩子,程序过不去呢,望大佬们解答,谢谢
点赞 回复
分享
发布于 2020-07-26 01:22

相关推荐

64 2 评论
分享
牛客网
牛客企业服务