_ Wins the binary tree and binary offer_ path certain value

_ The binary tree and binary tree path a certain value

Title Description
root input an integer and a binary tree, the binary tree paths prints out all of the nodes and the value for the input integer. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)

Answers

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回二维列表,内部每个列表表示找到的路径
    def __init__(self):
        # 相当于global作用,定义全局变量
        self.pathone = []
        self.pathall = []
        
    def FindPath(self, root, expectNumber):
        # write code here
        if not root:
            return self.pathall
        self.pathone.append(root.val)
        if not root.left and not root.right and root.val == expectNumber:
            self.pathall.append(self.pathone[:])
        if expectNumber-root.val>0:
            self.FindPath(root.left, expectNumber-root.val)
            self.FindPath(root.right, expectNumber-root.val)
        self.pathone.pop()
        return self.pathall
Published 31 original articles · won praise 0 · Views 715

Guess you like

Origin blog.csdn.net/freedomUSTB/article/details/105164509